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

thedeemon via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 5 08:44:02 PDT 2015


On Thursday, 3 September 2015 at 16:50:21 UTC, 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?

I think the Task and taskPool from std.parallelism are a good fit.
Something like:

auto task1 = task!fun1(params1);
auto task2 = task!fun2(params2);
auto task3 = task!fun3(params3);
auto task4 = task!fun4(params4);

taskPool.put(task1);
taskPool.put(task2);
taskPool.put(task3);
taskPool.put(task4);

auto res1 = task1.workForce();
auto res2 = task2.workForce();
auto res3 = task3.workForce();
auto res4 = task4.workForce();

//here we have all the results, they were calculated in parallel



More information about the Digitalmars-d-learn mailing list