Parallel For loops. Are they wait for finish?

17,336

Yes. Parallel.For will not return until all operations have completed.

If you run

Parallel.For(0, 5, i => Console.WriteLine("First {0}", i));
Console.WriteLine("First Finished");
Parallel.For(0, 5, i => Console.WriteLine("Second {0}", i));
Console.WriteLine("Second Finished");

The output is

First 0
First 2
First 1
First 4
First 3
First Finished
Second 0
Second 4
Second 3
Second 2
Second 1
Second Finished

The order of the integers will vary, but second will always come after first.

Share:
17,336

Related videos on Youtube

Mahdi Ghiasi
Author by

Mahdi Ghiasi

Updated on June 04, 2022

Comments

  • Mahdi Ghiasi
    Mahdi Ghiasi almost 2 years

    I have two for loops. which the second loop must be started after finishing the first loop.

    So, If I use two Parallel.For() loops, will the second loop runs after the finishing the first loop?

  • George Duckett
    George Duckett over 12 years
    Makes sense when you think about it too as the method returns a ParallelLoopResult, which would only be available once all threads had terminated.
  • Ray
    Ray over 12 years
    @GeorgeDuckett Indeed. It makes sense and is easy to verify.
  • Ray
    Ray over 12 years
    @Mahdi Google it. It means it can be called from multiple threads without risk of unexpected stuff happening. e.g. if it wasn't thread safe you might get the console lines jumbled together e.g. FiFrist r0st 1. Also if you're doing parallel stuff without knowing what thread safe is, you really really need to look it up.
  • MikeT
    MikeT over 9 years
    you make one mistake in your answer Parallel.For returns when all tasks have ended, this end is not always when it completed, it could also be error or interrupted
  • BillHaggerty
    BillHaggerty over 6 years
    What about 'Parallel.ForEach' is that the same?
  • Ray
    Ray over 6 years
    @Theyouthis It's easy to verify - just try it. I would be very surprised if it wasn't the same.