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?
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.
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.
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...
来源:https://stackoverflow.com/questions/7193698/when-a-concurrentbag-is-better-than-a-list