When a ConcurrentBag is better than a List?

2019-04-20 09:52发布

问题:

I am using a Parallel.Foreach for populating an external ConcurrentBag. I tried also to use a common List and everything works fine.

I have been lucky or I missed the special scope of ConcurrentBag?

回答1:

You have been lucky; Parallel.ForEach to populate a List is not thread-safe, you will eventually run into problems.

According to MSDN, List<T> is not thread safe:

Any instance members are not guaranteed to be thread safe.

A List<T> can support multiple readers concurrently, as long as the collection is not modified. Enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with one or more write accesses, the only way to ensure thread safety is to lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

ConcurrentBag is what you should use for this, which is thread-safe for multiple readers and writers.



回答2:

If you're using Parallel.ForEach to populate a List<T> and everything is working just fine then you're simply getting lucky. The ForEach method can and will run your code on multiple threads so any communication outside the ForEach must be with objects that can handle concurrent updates. List<T> cannot but ConcurrentBag<T> can.



回答3:

ConcurrentBag is the correct answer, only in .NET 4.0 it is very slow. This has been fixed in .NET 4.5. See http://ayende.com/blog/156097/the-high-cost-of-concurrentbag-in-net-4-0

Both ConcurrentStack and ConcurrentQueue will also work in your situation...