Saturday, 15 June 2013

c# - Custom conditions Parallel.For -



c# - Custom conditions Parallel.For -

i know th next line

system.threading.tasks.parallel.for(0, 5, (j, u) => {});

equals

for (int j = 0; j < 5; j++){}

but what's parallel code for

for (int = 0; (i < x + y && < z); i++)

it sounds want parallel.while construct, doesn't exist. can approximate functionality using this:

public static void while( paralleloptions paralleloptions, func<bool> condition, action<parallelloopstate> body) { parallel.foreach(new infinitepartitioner(), paralleloptions, (ignored, loopstate) => { if (condition()) body(loopstate)); else loopstate.stop(); }); }

stephen toub has blog on msdn

edit: throw out option, can utilize loopstate break out of parallel loop based on our condition. (psuedo code here:

parallel.for(0, n, (i, loopstate) => {

// ...

if (!(i < x + y && > < z)) {

loopstate.break(); return;

} });

c# for-loop parallel-processing

No comments:

Post a Comment