title: Parallel ForEach Giving different results in different ocasions
body: <p>I have never used <code>Parallel.ForEach</code> but I played around with it and found this occurrence.</p>
  
  <p>I run a parallel loop (code found on msdn <a href="https://msdn.microsoft.com/en-us/library/dd997393(v=vs.110).aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/dd997393(v=vs.110).aspx</a> did edit it with the <code>subtotal *=2</code> to try understand what it is doing) with an enumerable range firstly <code>(0,1)</code> and then <code>(0,1,2)</code> then I run the second one again but after sleeping the thread for 200 milliseconds, and the result then differs</p>
  
  <p>if the <code>Thread.sleep(200)</code>is not commented out this is the results</p>
  
  <pre><code>result 1 = 2
  result 2 = 6
  result 3 = 4
  </code></pre>
  
  <p>if the <code>Thread.sleep(200)</code>is commented out this is the results</p>
  
  <pre><code>result 1 = 2 
  result 2 = 6
  result 3 = 6
  </code></pre>
  
  <p>Here is the code</p>
  
  <pre><code>Stopwatch timer = new Stopwatch();
  int[] nums = Enumerable.Range(0, 1).ToArray();
  long total = 0;
  for (int i = 0; i &lt; 2; i++)
  {
       timer.Restart();
       total = 0;
       if (i == 0) nums = Enumerable.Range(0, 1).ToArray();
       if (i == 1) nums = Enumerable.Range(0, 2).ToArray();
       Parallel.ForEach&lt;int, long&gt;(nums,() =&gt; 0,(j, loop, subtotal) =&gt;
       {
         subtotal += 1;
         subtotal *= 2;
         return subtotal;
       },(finalResult) =&gt; Interlocked.Add(ref total, finalResult)); 
  
       Console.WriteLine("The total from Parallel.ForEach is {0:N0} and took {1}", total, timer.Elapsed);
       timer.Stop();
       //Thread.Sleep(200);
  }
  
  timer.Restart();
  nums = Enumerable.Range(0, 2).ToArray();
  total = 0;
  Parallel.ForEach&lt;int, long&gt;(nums, () =&gt; 0,  (j, loop, subtotal) =&gt;
  {
      subtotal += 1;
      subtotal *= 2;
      return subtotal;
  },(finalResult) =&gt; Interlocked.Add(ref total, finalResult)); 
  
  Console.WriteLine("The total from Parallel.ForEach is {0:N0} and took {1}", total, timer.Elapsed);
  timer.Stop();
  </code></pre>
  
  <p>I figured it has got to do with threads working over one another but this seems like a bug<br></p>
  
  <p>Note I did have a look at <a href="https://stackoverflow.com/questions/8944915/simulation-gives-different-result-with-normal-for-loop-vs-parallel-for?rq=1">Simulation gives different result with normal for loop Vs Parallel For</a> <br>
  Why is this happening?</p>