TPL - Parallel with Thread Local Variable

Scenario:

Process data in parallel and store data for each thread in a local variable and then keep adding it to class local variable in thread safe.

Solution:

Use thread-local variables to store and retrieve state in each separate task that is created by a for loop.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    public class ParallelWithThreadLocalVariable
        {
            static void Main(string[] args)
            {
                var rnd = new Random();
    
                int[] count = Enumerable.Range(0, 1000).ToArray();
                long totalCount = 0;
    
                //() => 0 initializes the thread-local variable to zero.
                //j: loop counter
                //loop: ParallelLoopState
                //subtotal: thread-local variable (returned by lambda expression)
                Parallel.For<long>(0, count.Length, () => 0, (j, loop, subtotal) =>
                {
                    int delay;
                    lock (rnd)
                        delay = rnd.Next(1, 1000);
                    Thread.Sleep(delay);
    
                    if (loop.ShouldExitCurrentIteration)
                    {
                        // if break has been called then any thread processing j > 500 will be called.
                        if (loop.LowestBreakIteration < j)
                            return subtotal;
                    }
    
                    if (j == 100)
                    {
                        Console.WriteLine($"Breaking at {j}");
                        loop.Break();
                    }
    
                    subtotal += count[j];
                    return subtotal;
                },
                //called  after all the iterations on a thread have completed.
                    (x) => Interlocked.Add(ref totalCount, x)
                );
    
                Console.WriteLine($"{totalCount}");
                Console.ReadLine();
            }
        }

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...