I want to use parallel for in a statement instead of while statement. When i look at samples Parallel For runs with only a known -or variable- count.
But i don’t know how many times my loop will run, and its unable to link it to a variable in run time.
I’m gonna try a simple performance test with TPL and classic code. So i’m writing a modulus class which calculates modulus with decrement operation. My function is like
long FindModulus(long n, int i)
{
while ( n >= i )
n -= i;
return n;
}
My goal is replacing this loop with a Parallel For loop
and i also want to learn can i use Parallel For with an if and break statement.
I think i will need a lock because value of n will be changed in all threads, any code sample would be appreciated
Thanks in advance
Since you’re not doing any work inside the loop any example would be contrived. But if you insist, here is an example that conveys the idea (it will be slower than the synchronous version because the synchronization overhead is larger than the work itself):