Reputation: 6048
We're using Parallel.foreach to consume items from a blocking collection. I know that by default partioning happens when this is done. I'd like to understand more how this may affect locks/blocking on a BlockingCollection. Is there any cases where an add operation on the blocking collection could be blocked when threads are consuming?
thanks
Upvotes: 3
Views: 3802
Reputation: 49978
It could (see How to: Add and Take Items Individually from a BlockingCollection)
This first example shows how to add and take items so that the operations will block if the collection is either temporarily empty (when taking) or at maximum capacity (when adding), or a specified timeout period has elapsed. Note that blocking on maximum capacity is only enabled when the BlockingCollection has been created with a maximum capacity specified in the constructor.
So it will block on adding if the collection is at maximum capacity.
If you consume the items in a foreach loop, adding items will not block during this, but there are a couple of things you will need to note:
Upvotes: 3