Difference between __gshared and shared.
Jonathan M Davis via Digitalmars-d
digitalmars-d at puremagic.com
Thu Jul 9 08:38:46 PDT 2015
On Thursday, 9 July 2015 at 14:57:56 UTC, Márcio Martins wrote:
> On Thursday, 9 July 2015 at 14:03:18 UTC, Jonathan M Davis
> wrote:
>> On Thursday, 9 July 2015 at 12:39:00 UTC, Márcio Martins wrote:
>>>[...]
>> Well, the compiler is free to assume that a variable that is
>> not marked as shared is thread-local. So, it's free to make
>> optimizations based on that. So, for instance, it can know for
>> a fact that
>>
>> [...]
>
> But this is what a C/C++ compiler would do, unless you your
> data is qualified as volatile. I believe __gshared also implies
> the volatile behavior, right? I wouldn't make sense any other
> way.
>
> So basically, __gshared is like saying "I want the C/C++
> behavior, and I accept I am all on my own as the compiler will
> not help me".
Sort of, but the assumptions that the D compiler is allowed to
make aren't the same. Regardless of shared/__gshared itself, D's
const is very different, and C++ doesn't have const or immutable.
And the D compiler devs can add whatever optimizations they want
based on what those features guarantee so long as they can prove
that they're correct, which changes what what a D compiler is
allowed to optimize in comparison to a C++ compiler. So, if you
make assumptions on what's valid based purely on C++, you risk
shooting yourself in the foot.
__gshared is really only meant for interacting with C APIs, and
if you're using it for other stuff, you're just begging for
trouble. You might get away with it at least some of the time,
but it really isn't a good idea to try.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list