std.parallelism: How to wait all tasks finished?

Andrea Fontana nospam at example.com
Thu Feb 6 03:30:16 PST 2014


On Wednesday, 5 February 2014 at 15:38:14 UTC, Cooler wrote:
> On Tuesday, 4 February 2014 at 03:26:04 UTC, Dan Killebrew 
> wrote:
>>>> It seems to me that worker threads will continue as long as 
>>>> the queue isn't empty. So if a task adds another task to the 
>>>> pool, some worker will process the newly enqueued task.
>>>
>>> No. After taskPool.finish() no way to add new tasks to the 
>>> queue. taskPool.put will not add new tasks.
>>
>> Then perhaps you need to create a new TaskPool (and make sure 
>> that workers add their tasks to the correct task pool), so 
>> that you can wait on the first task pool, then wait on the 
>> second task pool, etc.
>>
>> auto phase1 = new TaskPool();
>> //make sure all new tasks are added to phase1
>> phase1.finish(true);
>>
>> auto phase2 = new TaskPool();
>> //make sure all new tasks are added to phase2
>> phase2.finish(true);
>
> Will not help. I don't know beforehand what tasks will be
> created. procData is recursive and it decides create new task or
> not.


Something like this? (not tested...)

shared bool more = true;
...
...
...

void procData(){
   if(...)
   {
     taskPool.put(task(&procData));
     more = true;
   }
}

while(true)
{
    taskPool.finish(true);
    if (!more) break;
    else more = false;
}




More information about the Digitalmars-d-learn mailing list