我对这个例子感到困惑
http://msdn.microsoft.com/en-us/library/dd997393.aspx
Parallel.ForEach<int, long>(nums, // source collection
() => 0, // method to initialize the local variable
(j, loop, subtotal) =>
{
subtotal += nums[j];
return subtotal;
},
(finalResult) => Interlocked.Add(ref total,finalResult) );
我不知道为什么最后一个委托(finalResult) => Interlocked.Add(ref total,finalResult)
需要互锁,而前面的表达式
(j, loop, subtotal) =>
{
subtotal += nums[j];
return subtotal;
},
才不是?
谢谢