title: Parallel-For loop giving different sum results than single for loop. What am I missing?
body: <p>We have an obscure bug in one of our programs. I've narrowed at least part of it down to this parallel-for loop.</p>
  
  <p>i is an int[], for testing purposes, filled with ints 0 - 99999.
  runningTotal is a long.
  lockObject is a new Object();</p>
  
  <p>The Parallel-For loop always seems to return the total 704,982,704 when complete; the single-threaded loop returns 4,999,950,000.</p>
  
  <p>So obviously a threading issue, but I just don't see the mistake. Is the Parallel-For loop implemented properly?</p>
  
  <p>Relevant code below:</p>
  
  <pre><code>//i int[] test ---------------------------------------------
  object lockObject = new object();
  int[] i = new int[100000];
  for (int x = 0; x &lt; i.Length; x++)
  {
      i[x] = x;
  }
  long runningTotal = 0;
  
  Parallel.For(0, i.Length,
  () =&gt; 0,
  (x, loopState, subtotal) =&gt;
  {
      subtotal += i[x];
      return subtotal;
  },
  (s) =&gt;
  {
      lock (lockObject)
      {
          runningTotal += s;
      }
  }
  );
  runningTotal = 0;
  for (int x = 0; x &lt; i.Length; x++)
  {
      runningTotal += i[x];
  }
  </code></pre>