Parallel. To interrupt

Suppose you have an array of 1000 random integers, and you need to loop around on it to find the number 68, for example.

Using the new Parallel.For for a quad-core processor will significantly improve the speed, forcing each core to work with only 250 array elements.

The question arises: is it possible to interrupt the Parallel.For loop when the following condition is satisfied?

if (integerArray[i] == 68) break; 

Thanks.

+7
c # parallel-processing parallel-extensions
source share
2 answers

If you want to stop after executing the current executable iteration (but iterating until the current WILL, i.e. iteration with a lower index = i)

 Parallel.For(0, 100, (i, s) => { s.Break(); }); 

or if you want to stop after the current and intercoats before (in terms of index = i), you should also stop

 Parallel.For(0, 100, (i, s) => { s.Stop(); }); 

but in cases of BOTH, it’s good practice to check if work should be interrupted if iteration can take some time

 s.ShouldExitCurrentIteration 

more details here Parallel.For Method (Int32, Int32, Action (Int32, ParallelLoopState))

+9
source share

It seems to me that you should look into PLINQ (Parallel LINQ) to execute a parallel query, not a parallel one for.
http://msdn.microsoft.com/en-us/library/dd460688.aspx

+2
source share

All Articles