2

I have the following code inside a bigger loop, after profiling my code 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(); 
}); 
abenci
  • 8,422
  • 19
  • 69
  • 134
  • 1
    Please add the code for the outer loop. Also bear in mind that Stop() might be waiting for all threads to join? (I'm guessing here). – OJ. Aug 30 '10 at 11:01
  • 1
    That's not my experience, and in any case I would expect Parallel.For to be optimized for parallel execution and not fast cancellation. – Brian Rasmussen Aug 30 '10 at 11:14
  • What is your code doing? the piece you provided is useless. what are a and b, what is 1000? please post more code with description – Andrey Aug 30 '10 at 11:19
  • @Brian: You are probably right, but in my scenario having to call the `loopState.Stop()` many times makes a big difference. – abenci Aug 30 '10 at 12:12
  • The outer loop is simply a - not parallel - for loop. – abenci Aug 30 '10 at 12:13

1 Answers1

3

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

Alex Reitbort
  • 13,504
  • 1
  • 40
  • 61
  • this is correct answer! Stop is cooperative, means that threads should stop manually if IsStopped returns true. Break is imperative for all and doesn't require cooperation. – Andrey Aug 30 '10 at 11:23
  • Stop() should be faster as you can check here: msdn.microsoft.com/en-us/library/dd782827.aspx – abenci Aug 30 '10 at 12:16
  • 1
    @devdept: May be, but you must write code to pool IsStopped flag for it to work. Break just works. Do no take sentences out of context. – Alex Reitbort Aug 30 '10 at 12:17
  • @Andrey: Break will allow scheduled iterations to complete after it is called. Stop will allow running iterations to complete, but it will not schedule additional iterations after it is called. – Brian Rasmussen Aug 30 '10 at 12:32
  • @Brian Rasmussen i am not sure that i understood the difference. could you explain? – Andrey Aug 30 '10 at 12:39
  • @Andrey. Say you have a parallel loop that runs from 0 to 1000. If you break at index 100, Break will ensure that all iterations < 100 are completed before breaking. Stop on the other hand just stops the loop as early as possible. – Brian Rasmussen Aug 30 '10 at 12:45
  • @Brian Rasmussen The only possible way to break "as early as possible" is to call Thread.Abort, i know no other ways to break executing code. – Andrey Aug 30 '10 at 13:16
  • @Andrey. By "as early as possible" I meant "at the system's earliest convenience" as is the wording used by the documentation (http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate.break.aspx). The point being, that Break and Stop have very different semantics and that Break will actually allow further iterations to be scheduled. – Brian Rasmussen Aug 30 '10 at 13:38
  • Alternatively you can throw an exception - though not as clean – Lawrence Phillips May 18 '15 at 12:47