spawn and wait

Puming via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 2 20:29:57 PDT 2014


Hi,

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

```d
void task(int id)
{
   // do the stuff
}

void main()
{
   foreach (i; 0..10) {
     spawn(&task, i);
   }
   wait(?); // wait for all task to complete
   doSomeOtherThings();
}
```

But I don't see a `wait` method for Tid, similar to Pid in 
std.process.

What is the idiomatic way to do these things?

My current workaround is using messages:

```d
#!/usr/bin/rdmd
import std.stdio;
import std.concurrency;

void child(int id)
{
	writeln("Starting child: ", id);
	ownerTid.send(id);
}

void main()
{
	foreach (i; 0..10)
	{
		spawn(&child, i);
	}
	for (int n = 0; n < 10; ++n) {
		receive((int i) {
			writeln("Received:", i);
		});
	}
}
```

But it is verbose and seems error prone.


More information about the Digitalmars-d-learn mailing list