| Controlling access to a variable in a Parallel.For loop It is not sufficient to just do sum = sum + i without the lock because the read-modify-write operation is not atomic. A thread will overwrite any external modifications to sum that occur after it has read the current value of sum, but before it stores the modified value of sum + i back into sum. |