Asynchronous concurrency with reference types

Peter Alexander peter.alexander.au at gmail.com
Fri Feb 4 10:18:50 PST 2011


I would like to be able to spawn a thread, which does a lot of work, and 
then returns (in some way) the result of its computations to the main 
thread. I would really like to avoid copying its results if possible.

Logically, what I would like is something like this:


class LotsOfData { ... }

void doStuff(LotsOfData d) { ... }

shared(LotsOfData[]) data;

void main()
{
   spawn( &doWork );
   while(true)
   {
     foreach (d; data)
       doStuff(d);
   }
}

void doWork()
{
   LotsOfData d = new LotsOfData;
   // lots of computation on d
   data ~= d;
}


Essentially, the work that doWork does needs to be returned to the main 
thread asynchronously, and obviously in a thread-safe manner.

What's the best way to do this? The above won't work because ~= isn't 
atomic, so you can't do it on shared data.


More information about the Digitalmars-d-learn mailing list