pass array of objects to spawn

Timon Gehr timon.gehr at gmx.ch
Thu Nov 10 14:48:52 PST 2011


On 11/10/2011 11:00 PM, Ruslan Mullakhmetov wrote:
> On 2011-11-11 01:23:01 +0400, Timon Gehr said:
>
>> class Foo
>> {
>>
>> }
>>
>> void worker( shared(Foo[]) data_ )
>> {
>> Foo[] data = cast() data_; // this cast is valid because data_ is
>> never read from another thread after the cast
>> //...
>> }
>>
>> void main()
>> {
>> {
>> auto data = new Foo[10];
>> spawn( &worker, cast(shared)data ); // this cast is valid because data
>> is an unique reference (therefore there are no unshared aliases)
>> } // the sole reference to data in the main thread dies -> it will
>> never be read from this thread again
>> }
>
> Thank you too. Unfortunately i got compilation error
>
> thread.d(16): Error: cannot implicitly convert expression (data_) of
> type shared(Foo)[] to Foo[]
>
>
>

Interesting, apparently cast() does not remove shared. Sorry about that, 
use this (this time I tested it).

import std.concurrency;

class Foo
{

}

void worker( shared(Foo[]) data_ )
{
     auto data = cast(Foo[]) data_;
     //...
}

void main()
{
     {
         auto data = new Foo[10];
         spawn( &worker, cast(shared)data );
     }
}





More information about the Digitalmars-d-learn mailing list