I have a "single producer/single consumer" scenario implemented with new BlockingCollection of .NET 4.0.

The problem is that the producer thread awakes as soon as there's as a single space beocmes free in the collection. I want the producer to block until the consumer consumes at least half of the collection items. This is because producer speed is high and producing is expensive to the system.

How can I control the blocking condition for the producer?

1

There are 1 answers

2
Tim Lloyd On BEST ANSWER

An approach to consider is queuing fewer "bigger" items, rather than lots of "small" single items.

For example you could change the collection bound to 1 and change the item type to a list of items. In this way your producer could produce a list of 100 items and queue it, then the consumer would take this list and process it, leaving the producer to start on the next 100 items. The key here is that the producer will be optimized for producing larger batches of data in one batch before it is blocked waiting for the consumer to finish. There will be far less thrashing on the collection, but producing and consuming will still be overlapped.

When the consumer takes the list of 100 items, it is in effect taking half the total possible number of outstanding items i.e. 200 items. So conceptually this gives you your blocking condition.