Difference between __gshared and shared.

deadalnix via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 8 14:15:17 PDT 2015


On Wednesday, 8 July 2015 at 12:08:37 UTC, Jonathan M Davis wrote:
> By using __gshared, you're throwing away the compiler's help, 
> and it's _much_ more likely that you're going to write code 
> which causes the compiler to generate incorrect machine code, 
> because it's assuming that an object is thread-local when it's 
> not.
>
> Generally what you have to do with shared is lock on a mutex, 
> cast away shared on the object you want to operate on, do 
> whatever you're going to do with it, and then release the lock 
> after there are no more thread-local references to the shared 
> object. And that's basically what you normally should be doing 
> in C++ code except that you don't have to cast away shared, 
> because C++ doesn't have it.
>
> I know that there are a number of people who get frustrated 
> with shared and using __gshared instead, but unless you fully 
> understand what you're doing and how the language works, and 
> you're _really_ careful, you're going to shoot yourself in the 
> foot it subtle ways if you do that.
>
> - Jonathan M Davis

Amen


More information about the Digitalmars-d mailing list