BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Parallel and Asynchronous
.NET 4.0+

Terminating Parallel Loops

The fourth part of the Parallel Programming in .NET tutorial looks at the early termination of parallel loops. Unlike sequential loops, which can be exited with the break command, there are considerations when stopping loops that run on multiple cores.

LowestBreakIteration

We can see how the maximum iteration number is affected by calls to the Break method by viewing the LowestBreakIteration property of the ParallelLoopState object, as shown below:

Parallel.For(1, 20, (i, pls) =>
{
    Console.WriteLine(string.Format(
        "i={0} LowestBreakIteration={1}", i, pls.LowestBreakIteration));
    if (i >= 15)
    {
        pls.Break();
    }
});

/* OUTPUT
             
i=10 LowestBreakIteration=
i=11 LowestBreakIteration=
i=19 LowestBreakIteration=
i=1 LowestBreakIteration=
i=2 LowestBreakIteration=19
i=3 LowestBreakIteration=19
i=6 LowestBreakIteration=19
i=7 LowestBreakIteration=19
i=8 LowestBreakIteration=19
i=9 LowestBreakIteration=19
i=12 LowestBreakIteration=19
i=13 LowestBreakIteration=19
i=14 LowestBreakIteration=19
i=15 LowestBreakIteration=19
i=4 LowestBreakIteration=19
i=5 LowestBreakIteration=15
             
*/

When we view the results of the loop we can see that the LowestBreakIteration began with a null value. When one core reached the Break method at iteration 19, the property value changed. Note that no other core saw this change until iteration 2. Later, when iteration 15 executed, the property value was reduced to 15 by the second call to Break. Two further iterations still occurred to ensure that iterations 4 and 5 were completed.

Using ParallelLoopState.Stop

In some situations you will want your parallel loop to exit as quickly as possible, without attempting to mimic the results of a sequential loop. In these cases you can use the ParallelLoopState.Stop method. As with the Break method, iterations already executing in parallel will complete before the loop finally stops.

The following sample shows the use of the Stop method. Here we are executing a loop for each of the values between one and twenty. When we find a value that divides by six without remainder, the Stop method is called.

Parallel.For(1, 20, (i, pls) =>
{
    Console.Write(i + " ");
    if (i % 6 == 0)
    {
        Console.WriteLine("Stop on {0}", i);
        pls.Stop();
    }
});

/* OUTPUT

1 10 19 2 3 6 Stop on 6
11 4

*/

In the results shown in the code comments you can see that one core called Stop when the number six was found. Iterations 11 and 4 were already scheduled so these finished before the loop was complete. The key difference here is that iteration 5 was never encountered, as it would have been if Break was used instead of Stop. Note that it is still possible for multiple iterations to call the Stop method.

26 August 2011