Difference between __gshared and shared.

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 8 11:11:22 PDT 2015


On 7/8/15 5:20 AM, wobbles wrote:
> After reading the recent "Lessons Learned" article [1], and reading a
> few comments on the thread, there was a mention of using __gshared over
> shared.
>

I don't see any full answers to this so:

> What exactly is the difference here?

__gshared just puts the data in global segment, but does NOT alter the 
type. shared DOES alter the type:

__gshared int x1;
shared int x2;

pragma(msg, typeof(x1)); // int
pragma(msg, typeof(x2)); // shared(int)

Why is this important? Because you can overload on shared data to do 
special things (i.e. reject pointers to shared data, or use mutex locks 
around only truly shared data). __gshared data is only strictly a 
storage class, so you cannot do anything different with e.g. &x1 as you 
could with an address to a normal thread-local int.

As has been mentioned, __gshared data more accurately represents how C 
treats global data, but technically, you could use C to access shared 
variables. Both would have to be tagged with extern(C).

> Is there plans to 'converge' them at some point?

No, they are different concepts.

-Steve


More information about the Digitalmars-d mailing list