spawn X different workers & wait for results from all of them

Justin Whear via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Sep 4 10:32:48 PDT 2015


On Thu, 03 Sep 2015 18:50:21 +0200, Robert M. Münch wrote:

> Hi, I'm not sure how to best implement the following:
> 
> 1. I have 4 different tasks to do.
> 2. All can run in parallel 3. Every task will return some result that I
> need
> 
> Now how to best do it? When using receive() it fires on the first hit
> and the function continues. It's like a receive(OR), one hit and it's
> done. So, I would need something like a receive(ALL) that continues only
> of all results (messages) have been received.
> 
> Is something like this available or do I have to build it myself?

How would receive know?  If you're using std.concurrency, the receiving 
function needs to encode doneness, e.g.

const numJobs = 4;
foreach (_; 0 .. numJobs)
	receive(...);

Or you could use std.parallelism:

foreach (pieceOfWork; parallel(listOfWork))
	doIt(pieceOfWork);


More information about the Digitalmars-d-learn mailing list