[dmd-concurrency] tail-shared by default?

Steve Schveighoffer schveiguy at yahoo.com
Fri Jan 8 19:43:38 PST 2010


----- Original Message ----

> From: Walter Bright <walter at digitalmars.com>
> 
> All that's necessary is to take the address of a shared local and pass it to 
> another thread:
> 
>   void foo()
>   { shared int x;
>      passToAnotherThread(&x);
>      x++;     // <=== synchronization issues!
>      waitForThreadToFinish();
>    }
> 

In my scheme the declaration of x above is illegal.  You cannot share stack data.

Even if you have:

void foo()
{
   shared int * x; // ok, equivalent to shared(int)*
   passToAnotherThread(&x); // error, &x is shared(int)**, cannot convert to shared (int *)*
}

basically, it's impossible to have a fully shared local variable, all shared local variables are tail-shared.  The only place fully shared data can be is global data or the heap.  This is in essence exactly how it should be -- you are not sharing the pointer that you copy onto the stack, you are sharing what it points to.

-Steve



      


More information about the dmd-concurrency mailing list