Using 'AsParallel()' / 'Parallel.ForEach()' guidelines?

前端 未结 4 1863
说谎
说谎 2021-01-30 20:52

Looking for a little advice on leveraging AsParallel() or Parallel.ForEach() to speed this up.

See the method I\'ve got (simplified/bastardized

4条回答
  •  时光取名叫无心
    2021-01-30 20:54

    When using AsParallel(), you need to make sure that your body is thread safe. Unfortunately, the above code will not work. List is not thread safe, so your addition of AsParallel() will cause a race condition.

    If, however, you switch your collections to using a collection in System.Collections.Concurrent, such as ConcurrentBag, the above code will most likely work.

提交回复
热议问题