Parallel class (basics)

The parallel class can be found in the System.Threading.Tasks namespace. It has a couple of static methods designed to execute code concurrently. It does make sense to use parallelism when your code length justifies the creation of tasks, the code does not block each other too much (eg. lock(this) {}), the processors have free capacities and the code does not have to run in a sequence. Otherwise the performance does most likely suffer.

Let’s have a look at some examples:

public static void Parallel_For() {
    Parallel.For(0, 10, i => {
        Console.WriteLine("parallel start " + i);
        Thread.Sleep(0);
        Console.WriteLine("parallel end " + i);
    });
    Console.ReadLine();
} //

example output:
parallel start 0
parallel start 1
parallel end 0
parallel start 2
parallel end 1
parallel start 3
parallel end 3
parallel start 4
parallel end 2
parallel start 5
parallel end 4
parallel start 6
parallel end 5
parallel end 6
parallel start 8
parallel start 7
parallel start 9
parallel end 9
parallel end 8
parallel end 7

public static void Parallel_ForEach() {
    int[] n = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    Parallel.ForEach(n, i => {
        Console.WriteLine("parallel start " + i);
        Thread.Sleep(0);
        Console.WriteLine("parallel end " + i);
    });
    Console.ReadLine();
} //

example output:
parallel start 0
parallel end 0
parallel start 3
parallel start 4
parallel end 4
parallel start 5
parallel start 1
parallel start 2
parallel end 3
parallel start 6
parallel end 1
parallel start 7
parallel end 2
parallel start 8
parallel end 8
parallel start 9
parallel end 9
parallel end 6
parallel end 5
parallel end 7

public static void Parallel_ForBreak() {
    ParallelLoopResult result = Parallel.For(0, 10, (i, loopstate) => {
        Console.WriteLine("parallel start " + i);
        Thread.Sleep(0);
        if (i >= 5) loopstate.Break();
        //if (i >= 3) loopstate.Stop();
        Console.WriteLine("parallel end " + i);                                
    });
    Console.WriteLine("IsCompleted: " + result.IsCompleted);
    Console.WriteLine("LowestBreakIteration: " + result.LowestBreakIteration);
    Console.ReadLine();
} //

example output:
parallel start 0
parallel end 0
parallel start 2
parallel end 2
parallel start 3
parallel start 1
parallel end 1
parallel end 3
parallel start 6
parallel end 6
parallel start 7
parallel start 4
parallel end 7
parallel end 4
parallel start 5
parallel end 5
IsCompleted: False
LowestBreakIteration: 5

The field IsCompleted returns false when the loop did not complete. And LowestBreakIteration represents the lowest iteration number from which the break statement was called. All lower iteration numbers are executed. The function does not break, the code after break will still be executed! The Break() statement can be employed in search-based algorithms where an ordering is present in the data source.

public static void Parallel_ForBreak() {
    ParallelLoopResult result = Parallel.For(0, 100, (i, loopstate) => {
        Console.WriteLine("parallel start " + i);
        Thread.Sleep(0);
        //if (i >= 5) loopstate.Break();
        if (!loopstate.IsStopped) {
            if (i >= 25) loopstate.Stop();
            Console.WriteLine("parallel end " + i);
        }
    });
    Console.WriteLine("IsCompleted: " + result.IsCompleted);
    Console.WriteLine("LowestBreakIteration: " + result.LowestBreakIteration);
    Console.ReadLine();
} //

example output:
parallel start 0
parallel start 12
parallel end 0
parallel start 1
parallel end 1
parallel start 2
parallel end 2
parallel start 3
parallel end 3
parallel start 4
parallel end 4
parallel start 5
parallel end 5
parallel start 6
parallel start 24
parallel end 24
parallel start 25
parallel end 25
parallel end 12
IsCompleted: False
LowestBreakIteration:

The Stop() statement returns null in LowestBreakIteration. The code after Stop() is still executed!

Break() completes all iterations on all threads that are prior to the current iteration on the current thread, and then exit the loop.

Stop() stops all iterations as soon as convenient.

About Bastian M.K. Ohta

Happiness only real when shared.

Posted on December 5, 2013, in Basic, C#, Threading and tagged , , , , , . Bookmark the permalink. 2 Comments.

Leave a comment