spawn and wait

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 3 11:12:18 PDT 2014


On 07/02/2014 08:29 PM, Puming wrote:

 > I want to spawn several similar tasks and then wait for all of them to
 > complete to go on do some other things, like:

[...]

 > My current workaround is using messages:

I forgot to mention that if message passing is merely a "workaround" :) 
in this case then perhaps std.parallelism is more suitable.

For example, your code may be as simple as running a loop in .parallel 
in a foreach loop. The foreach loop would not advance until all of the 
parallel tasks have been completed:

import std.stdio;
import std.parallelism;
import std.range;

void task(size_t id)
{
     writefln("Working for %s", id);
}

void main()
{
     foreach (id; iota(10).parallel) {
         task(id);
     }

     writeln("All done");
}

Ali



More information about the Digitalmars-d-learn mailing list