shared - no read/write access

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Mar 31 07:05:01 UTC 2019


On Wednesday, March 27, 2019 2:58:06 PM MDT Rubn via Digitalmars-d wrote:
> On Wednesday, 27 March 2019 at 18:25:09 UTC, Kagamin wrote:
> > On Tuesday, 26 March 2019 at 21:09:18 UTC, Rubn wrote:
> >> __gshared int a;
> >>
> >> See above, you have an unqualified type. Would you say it is
> >> thread local? Is there any way you can see that it is thread
> >> local by the type alone?
> >
> > It has thread local type. It's an escape provided to ease
> > migration from other languages, when it's difficult for
> > whatever reason to have shared data qualified as shared.
>
> Is it thread local data? How do you identify it is not
> thread-local data by type only (it is unqualified)?

If you use __gshared, it's up to you to ensure that thread-safety is
guaranteed - just like you have to guarantee thread-safety when you cast
away shared to operate on a shared object (usually after having locked a
mutex to protect it). __gshared is really only intended for linking against
C globals (which aren't typed as shared, because C doesn't have shared), and
its use in any program should be minimal. Regardless, it's up to the
programmer to use it correctly, just like it's up to the programmer to
ensure that @trusted code is actually @safe, because as far as the compiler
is concerned, __gshared is not part of the type. So, any code using that
variable will assume that it's thread-local, leaving it up to the programmer
to protect it correctly, whereas with shared, the compiler is supposed to
prevent operations that aren't thread-safe (thus usually requiring shared to
be cast away to operate on the variable).

- Jonathan M Davis





More information about the Digitalmars-d mailing list