++ counter in Parallel.ForEach

I understand that using the ++ iterator inside Parallel.ForEach is not a good option, but now I have to use a counter inside a Parallel.ForEach loop, the counter is used to search for column names of a dynamic object at runtime. Any suggestion, what would be the best option ?. I read somewhere in StackOverflow that using "Blocked" is again a bad design inside Parallel.ForEach.

+3
source share
1 answer

If you really need parallel processing, indexes must be pre-calculated. Something like that Enumerable.Range(0, cols.Length).ToArray(). Otherwise, each column will depend on the previous one, which obviously does not parallelize.

+1
source

Source: https://habr.com/ru/post/1758911/


All Articles