www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Shouldn't spawn work with __gshared?

reply Andrej Mitrovic <none none.none> writes:
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.

bar could use the global directly, yes, but this was just a test case. In my
real code the bar function is defined in a different module and doesn't see the
foo instance since it's in module scope of the main module.

Anyway, yeah I'll translate this to a thread-safe version, of course. But this
looks like a bug to me.
Apr 20 2011
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 20 Apr 2011 18:10:35 -0400, Andrej Mitrovic <none none.none> 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
Apr 21 2011
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 21 Apr 2011 11:18:01 -0400, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 Oh I thought __gshared was in the type system. Yeah I'll try the
 Thread class, thanks.

__gshared puts the variable in global storage, just like D1 global variables. It does not mark the type at all. -Steve
Apr 21 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Oh I thought __gshared was in the type system. Yeah I'll try the
Thread class, thanks.
Apr 21 2011