title: Parallel.For(): Update variable outside of loop
body: <p>I'm just looking in to the new .NET 4.0 features. With that, I'm attempting a simple calculation using <code>Parallel.For</code> and a normal <code>for(x;x;x)</code> loop.  </p>
  
  <p>However, I'm getting different results about 50% of the time. </p>
  
  <pre><code>long sum = 0;
  
  Parallel.For(1, 10000, y =&gt;
      {
          sum += y;
      }
  );
  
  Console.WriteLine(sum.ToString());
  
  sum = 0;
  
  for (int y = 1; y &lt; 10000; y++)
  {
     sum += y;
  }
  Console.WriteLine(sum.ToString());
  </code></pre>
  
  <p>My guess is that the threads are trying to update "sum" at the same time.<br>
  Is there an obvious way around it?</p>