Reputation: 666
I have never used Parallel.ForEach
but I played around with it and found this occurrence.
I run a parallel loop (code found on msdn https://msdn.microsoft.com/en-us/library/dd997393(v=vs.110).aspx did edit it with the subtotal *=2
to try understand what it is doing) with an enumerable range firstly (0,1)
and then (0,1,2)
then I run the second one again but after sleeping the thread for 200 milliseconds, and the result then differs
if the Thread.sleep(200)
is not commented out this is the results
result 1 = 2
result 2 = 6
result 3 = 4
if the Thread.sleep(200)
is commented out this is the results
result 1 = 2
result 2 = 6
result 3 = 6
Here is the code
Stopwatch timer = new Stopwatch();
int[] nums = Enumerable.Range(0, 1).ToArray();
long total = 0;
for (int i = 0; i < 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<int, long>(nums,() => 0,(j, loop, subtotal) =>
{
subtotal += 1;
subtotal *= 2;
return subtotal;
},(finalResult) => 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<int, long>(nums, () => 0, (j, loop, subtotal) =>
{
subtotal += 1;
subtotal *= 2;
return subtotal;
},(finalResult) => Interlocked.Add(ref total, finalResult));
Console.WriteLine("The total from Parallel.ForEach is {0:N0} and took {1}", total, timer.Elapsed);
timer.Stop();
I figured it has got to do with threads working over one another but this seems like a bug
Note I did have a look at Simulation gives different result with normal for loop Vs Parallel For
Why is this happening?
Upvotes: 1
Views: 1690
Reputation: 239664
Because this code is ill-defined:
Parallel.ForEach<int, long>(nums,() => 0,(j, loop, subtotal) =>
{
subtotal += 1;
subtotal *= 2;
return subtotal;
},(finalResult) => Interlocked.Add(ref total, finalResult));
In that, if a single thread executes both iterations then you get the result 6. Effectively, you do:
subTotal = 0; //From init
subTotal += 1; //=1 First iteration
subTotal *= 2; //=2 First iteration
subTotal += 1; //=3 Second iteration
subTotal *= 2; //=6 Second iteration
total += subTotal; //=6 End gathering (actually interlocked)
But if two threads share the work, you get
subTotal1 = 0; //From init
subTotal2 = 0; //From init
subTotal2 += 1; //=1
subTotal1 += 1; //=1
subTotal1 *= 2; //=2
subTotal2 *= 2; //=2
total += subTotal1 //=2 End gathering 1 (interlocked)
total += subTotal2 //=4 End gathering 2 (interlocked)
Upvotes: 3