On Wed, 20 Apr 2011 18:10:35 -0400, Andrej Mitrovic <[email protected]> wrote:
Before you give me a lecture on thread safety, sometimes I have to test
code on-the-fly and __gshared comes in really handy when working with C
codebases.
Now, here's something that won't fly:
import std.concurrency;
class Foo { }
__gshared Foo foo;
void main()
{
foo = new Foo();
spawn(&bar, foo);
}
void bar(ref Foo)
{
}
Error:
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\concurrency.d(326): Error:
static assert "Aliases to mutable thread-local data not allowed."
spawnGshared.d(13): instantiated from here: spawn!(Foo)
foo isn't thread local.
foo is shared, but it's not *typed* as shared. Which means spawn is given
the type as just "Foo", not "__gshared Foo" or "shared Foo".
spawn is not a compiler-special function, it's just a normal function. It
has no idea that foo is __gshared, because all it has access to is what
the type system says.
You might try using the Thread class directly, and start threads the old
fashioned way. Although I'm not sure if you get message passing (you can
probably set up message passing manually though).
It might be nice to have an 'unsafeSpawn' for cases where you are handling
the concurrency issues manually, but still want to use the new API.
-Steve