Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsParallel.ForAll vs Parallel.ForEach

Is there any difference between the below code snippets. If so, what?

myList.AsParallel().ForAll(i => { /*DO SOMETHING*/ });

and

Parallel.ForEach(mylist, i => { /*DO SOMETHING*/ });

Will the main thread wait for all the child threads to complete? In a MVC application, if I'm doing parallel processing in my controller action, what happens to the child threads after the main thread completes. Will they be aborted or will they be completed even after the main thread is completed?

like image 237
Dhawal Avatar asked Jan 08 '13 01:01

Dhawal


People also ask

Which is faster parallel ForEach or ForEach?

The execution of Parallel. Foreach is faster than normal ForEach.

When should I use parallel ForEach When should I use Plinq?

Note, in this situation Parallel. ForEach expects you to store your results in a thread-safe manner, while PLINQ handles those details for you.

Is parallel ForEach good?

The short answer is no, you should not just use Parallel. ForEach or related constructs on each loop that you can. Parallel has some overhead, which is not justified in loops with few, fast iterations. Also, break is significantly more complex inside these loops.

What is the difference between ForEach and parallel ForEach in C#?

ForEach is like the foreach loop in C#, except the foreach loop runs on a single thread and processing take place sequentially, while the Parallel. ForEach loop runs on multiple threads and the processing takes place in a parallel manner.


2 Answers

Parallel.ForEach() is intended exactly for this kind of code.

On the other hand, ForAll() is intended to be used at the end of a (possibly complex) PLINQ query.

Because of that, I think Parallel.ForEach() is the better choice here.

In both cases, the current thread will be used to perform the computations (along with some threads from the thread pool) and the method will return only after all processing has been completed.

like image 148
svick Avatar answered Oct 13 '22 21:10

svick


Here is an explanation in MSDN:

https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/potential-pitfalls-with-plinq#prefer-forall-to-foreach-when-it-is-possible

Based on what I read, use Parallel.ForEach() if you want to ensure the list are access sequentially while using AsParallel.ForAll() does not guarantee the items in the list are accessed in order.

For MVC, all thread are request-based. The caller thread (main) is blocked until the Parallel() call is completed, that means all child threads should have completed as well.

If the caller thread is aborting, here is an explain:

http://www.albahari.com/threading/part5.aspx

PLINQ doesn't preemptively abort threads, because of the danger of doing so. Instead, upon cancellation it waits for each worker thread to finish with its current element before ending the query. This means that any external methods that the query calls will run to completion.

like image 33
dsum Avatar answered Oct 13 '22 20:10

dsum