When not used inside a static
context (that is, when the static
keyword isn't present, and you're not in global scope), what do the shared
and __gshared
keywords do?
Examples:
struct Temp
{
shared int i;
__gshared int j;
}
The shared int is typed shared(int)
, which doesn't do anything but give it that type. The __gshared int
is a no-op -- DMD is fond of ignoring no-op attributes.
D2 defaults to Thread Local Storage, while C, C++ and D1 default to global storage.
One of the differences is that a global variable in D is visible to other threads, while TLS is not.
This may not sound like much, but try interfacing to a C library without realizing this. (immutable is. global as well)
IME __gshared pretty much only exists to force something into global when normally it would not.
there may be other uses for it, but I haven't seen any.
An example would be a global variable in a C header. If you try to interface with it, you'll need immutable or __gshared. There are other ways of course, but this is probably easiest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With