I have the following scenario:
I have a Parallel.ForEach loop iterating over a collection of objects using a custom Partitioner. The partitioner takes a subset of 100 objects and sends them to a web service. The results returned need to be stored in a single collection. In other words all iterations of the Parallel.ForEach will write into a single collection.
Can I use a concurrent collection like ConcurrentBag for storing the results and be assured that the Parallel.ForEach iterations will not mutate the result in any way.
Thanks
Vikas
I'll address two parts of your question separately:
Can I use a concurrent collection like ConcurrentBag for storing the results?
Yes, ConcurrentBag<T>
is specifically designed to support concurrent Add
/Remove
/iteration operations. If you're calling Add
inside the Parallel.ForEach
delegate body, all of the items you add are guaranteed to be stored - although their exact order is undefined.
Can I ... be assured that the Parallel.ForEach iterations will not mutate the result in any way?
This depends on what it is exactly you do inside your Parallel.ForEach
delegate body. Like I said, if all you're doing there is storing the results in a ConcurrentBag<T>
(via a call to Add
somewhere in your Parallel.ForEach
delegate - that's the most common use case), you're safe.
If you are performing more than one operation on ConcurrentBag<T>
inside your Parallel.ForEach
delegate (i.e. reading from ConcurrentBag<T>
before you write to it), thread safety goes out the window.