On Saturday, 27 May 2017 at 10:29:05 UTC, Ola Fosheim Grøstad wrote:
On Saturday, 27 May 2017 at 10:02:35 UTC, Jonathan M Davis wrote:
As such, the fact that D programmers frequently decide to use __gshared in order to avoid dealing with the restrictions with shared is actually extremely bad. You can get away with it in some cases, but it's error-prone and is only going to get worse as the compiler improves.

Hm, I would think that using __gshared would not be affected by compiler improvements, since it would turn off optimizations that assume that the variable doesn't change between reads?

Nope. __gshared isn't even part of the type so it can't do that.

__gshared int a;

void main()
{
    foo(a);
}

void foo(ref int b)
{
    // No way of knowing that b actually
    // references __gshared memory.
}

__gshared is a way of declaring a C-like global variable, but D does not - in general - support accessing it in a C-like way. My advice is to cast to shared before use, unless you're totally sure you know what other threads will be doing.

Reply via email to