Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concurrent Collections and Unique elements

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

like image 689
B.Geronatsios Avatar asked Oct 04 '11 18:10

B.Geronatsios


1 Answers

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.

like image 199
Jim Mischel Avatar answered Nov 02 '22 05:11

Jim Mischel