Controlling access to a variable in a Parallel.For loop

suggest change
using System;
using System.Threading;
using System.Threading.Tasks;

class Program
{
    static void Main( string[] args )
    {
        object sync = new object();
        int sum = 0;
        Parallel.For( 1, 1000, ( i ) => {
            lock( sync ) sum = sum + i; // lock is necessary

            // As a practical matter, ensure this `parallel for` executes
            // on multiple threads by simulating a lengthy operation.
            Thread.Sleep( 1 );
        } );
        Console.WriteLine( "Correct answer should be 499500.  sum is: {0}", sum );
    }
}

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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Making a variable thread safe:
* Controlling access to a variable in a Parallel.For loop

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
115 Making a variable thread safe
127 Caching
135 Pointers
147 C# Script