Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Parallel.ForEach(task.Wait) and Task.WaitAll

What is the difference between:

Parallel.ForEach(sometasks, x => x.Wait());

and

Task.WaitAll(sometasks);

EDIT:

Well I am not fully sure why I included Parallel into the question :/ ... What I had in mind was:

foreach(Task task in someTasks)
{
    task.Wait();
}

but I just included Parallel.ForEach thinking it is the same thing, while of course its not --- it spawns a thread for each task to wait, which is big overhead.

like image 877
Denis Biondic Avatar asked Sep 18 '26 06:09

Denis Biondic


1 Answers

Though Parallel.ForEach(sometasks, x => x.Wait()); makes no sense for me.

Short answer is

Parallel.ForEach: With your code, blocks at least two threads. Calling thread and ThreadPool thread(s) as well.

Task.WaitAll: Blocks only the calling thread.

like image 175
Sriram Sakthivel Avatar answered Sep 19 '26 18:09

Sriram Sakthivel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!