I want to increment an index on a particular value, for example 2:
for (int i = 0; i < 10; i+=2)
{
Console.WriteLine(i);
}
How do I do the same using the Parallel class, like:
Parallel.For(0, 10, i =>
{
Console.WriteLine(i);
i += 2; //this a naïve assumption, it's not working
});
Edit
I would like the Parallel loop to run only 5 operations (as the sequential for) and order doesn’t matter for me.
The implicit assumption in the first loop is that j is incremented sequentially.
In the second example the value of j can be any of 0 -> 9 in any of the loops.
You can achieve the same behaviour by the following: