Counter++ in Parallel.ForEach
I understand using an iterator++ inside Parallel.ForEach is not a good option but right now i'm forced to use a counter inside a Par开发者_开发百科allel.ForEach loop, counter is used to pick up column names of a dynamic object at runtime.Any suggestion what would be the best option?.I read somewhere at StackOverflow that using "Interlocked" is again a bad design inside Parallel.ForEach.
If you really need parallel processing, the indices will have to be pre-computed. Something like Enumerable.Range(0, cols.Length).ToArray()
. Otherwise, each column will depend on the previous one, which obviously doesn't parallelize.
精彩评论