Is Parallel.ForEach in ConcurrentBag thread safe

前端 未结 2 2007
失恋的感觉
失恋的感觉 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:01

    ConcurrentBag and Parallel.ForEach seems to me, no problem. If you uses this types in scenarios that has a large volume multi-user access, these classes in your implementation could rise up cpu process to levels that can crash your web-server. Furthermore, this implementation starts N tasks (threads) to execute each one of iterations, so be careful when choice this classes ans implementations. I recently spent in this situation and I had to extract memory dump to analyze whats happens into my web application core. So, be careful 'cause Concurrentbag is ThreadSafe and in web scenarios it is no better way.

提交回复
热议问题