Is Parallel.ForEach in ConcurrentBag thread safe

前端 未结 2 2006
失恋的感觉
失恋的感觉 2021-02-20 04:21

Description of ConcurrentBag on MSDN is not clear:

Bags are useful for storing objects when ordering doesn\'t matter, and unlike sets, bags support duplicates. C

2条回答
  •  南旧
    南旧 (楼主)
    2021-02-20 05:14

    That looks fine to me. The way you're using it is thread-safe.

    If you could return an IEnumerable, it could be made more efficient by not copying to a List when you're done.

提交回复
热议问题