Concurrent collections and unique elements

blockingcollection, c#, concurrency, concurrent-collections, parallel-processing

Solution

The default backing store for `BlockingCollection` is a `ConcurrentQueue`. As somebody else pointed out, it's rather difficult to add distinct items using that.

However, you could create your own collection type that implements `IProducerConsumerCollection`, and pass that to the `BlockingCollection` constructor.

Imagine a `ConcurrentDictionary` that contains the keys of the items that are currently in the queue. To add an item, you call `TryAdd` on the dictionary first, and if the item isn't in the dictionary you add it, and also add it to the queue. `Take` (and `TryTake`) get the next item from the queue, remove it from the dictionary, and return.

I'd prefer if there was a concurrent `HashTable`, but since there isn't one, you'll have to do with `ConcurrentDictionary`.

Problem

I have a concurrent `BlockingCollection` with repeated elements. How can modify it to add or get distinct elements?

Original source

Related problems