Once you are comfortable using parallel loops, stopping them, and performing exception handling with them, let's talk about how we can use thread-local variables to better coordinate results. If we want to sum the results of all the iterations of a parallel loop, how would we do that?
From what we have learned so far, we would create a class variable before the loop and access it by each iteration of the loop using a
lock statement so that it remains thread-safe. This takes overhead and coordination time. To improve performance, we can implement our parallel loop using a thread-local variable.
Parallel.ForEach loops both have overloads that implement a thread-local variable. What ...