Parallel.For(): Update variable outside of loop

前端 未结 7 2093
面向向阳花
面向向阳花 2020-11-29 22:20

I\'m just looking in to the new .NET 4.0 features. With that, I\'m attempting a simple calculation using Parallel.For and a normal for(x;x;x) loop.

7条回答
  •  生来不讨喜
    2020-11-29 23:02

    if there are two parameters in this code. For example

    long sum1 = 0;
    long sum2 = 0;
    
    Parallel.For(1, 10000, y =>
        {
            sum1 += y;
            sum2=sum1*y;
        }
    );
    

    what will we do ? i am guessing that have to use array !

提交回复
热议问题