When I use Parallel.ForEach or Parallel.For in the System.Threading.Tasks namespace, can I assume that all threads are synchronised before execution continues?
Is there something equivalent to
WaitHandle.WaitAll(...);
going on?
So If I call something like
Parallel.ForEach(collection, DoSomethingWithObject);
Can I be certain that each call to DoSomethingWithObject has completed before ForEach returns, or do I need to use WaitHandles myself?
All the tasks will have completed by the time
Parallel.ForEach()returns, unless a call toStop()was specifically made to abort the parallel execution. In such cases, the.IsCompletedproperty of theParallelLoopStateobject returned byParallel.ForEach()will befalse.You can read about it here:
http://msdn.microsoft.com/en-us/library/dd992001.aspx
http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopresult.aspx
You can also write a small test application where
DoSomethingWithObject()is a very long running operation, or a call toThread.Sleep(), to verify this. Execution will block until all threads have finished sleeping.Sample: