By default, all parallel loops have access to a global variable. However, there is a synchronization overhead associated with accessing global variables, and because of this, it makes sense to use thread-scoped variables wherever possible. We can create either a thread local or a partition local variable to be used in parallel loops.
Understanding thread storage in parallel loops
Thread local variable
Thread local variables are like global variables for a particular task. They have a lifetime that spans the number of iterations the loop is going to execute.
In the following example, we are going to look at thread local variables using the for loop. In the case of the Parallel.For loop, multiple tasks are created to run the...