开发者

Slow Parallel.For interruption

开发者 https://www.devze.com 2023-01-13 21:36 出处:网络
I have the following code inside a bigger loop, after profiling my c开发者_如何学编程ode I discovered that all the Parallel.For gain in execution speed is lost in the long time the Stop() method takes

I have the following code inside a bigger loop, after profiling my c开发者_如何学编程ode I discovered that all the Parallel.For gain in execution speed is lost in the long time the Stop() method takes to complete. Is there any way to improve this? Maybe calling Thread.Sleep()?

Thanks.

Parallel.For(0, 1000, (i, loopState) => 
{ 
   if (a == b)
       loopState.Stop(); 
}); 


I think you should use loopState.Break() method, since it is paralel for break keyword. The Stop method sets IsStopped flag, so that other iterations may check this flag and stop at their convenience. It does not stop the loop.

See Stop and Break on msdn

0

精彩评论

暂无评论...
验证码 换一张
取 消