1
votes

A consumer thread and multiple producer threads synchronize their work with a System.Collections.Concurrent.BlockingCollection<T>.

The producers call blockingCollection.Add()
and the consumer runs
foreach (var item in blockingCollection.GetConsumingEnumerable()) {...}

Now one producer wants to "flush" the buffer: The producer wants to wait until all current items have been consumed. Other producers may add further items in the meantime, but this producer is only interested in the items that are currently in the queue.

How can I make the producer wait, without using busy waiting?

In essence I want some non-consumer threads to get notified whenever an item of the BlockingCollection is consumed .

I can set an AutoResetEvent in the consumer, but this will wake up only one thread waiting for changes, when there could be multiple:

foreach (var item in blockingCollection.GetConsumingEnumerable()) 
{
    myAutoResetEvent.Set()
}

I can also set a ManualResetEvent:

foreach (var item in blockingCollection.GetConsumingEnumerable()) 
{
    myManualResetEvent.Set()
}

This will wake up all waiting threads, but how would I switch it back off again?

1
Why do you need to wake up the consumers at all? If they're using GetConsumingEnumerable they'll already be waiting for new items in the sequence, and they'll start processing the new items as soon as there are new items to work on.Servy
I want a non-consumer thread to be notified. The consumers of course get their updates automatically. But now I want to notify some other threads that are not consuming items.HugoRune
Put a an dummy item in the collection that wakes up the producer.paparazzo
@Blam But if other items are added before that item is consumed, then the collection hasn't been emptied.Servy
@Servy That is not how I read it. "Other producers may add further items in the meantime, but this producer is only interested in the items that are currently in the queue."paparazzo

1 Answers

4
votes

In case you end up using my comment

Put a dummy item in the collection that wakes up the producer