views:

286

answers:

1

I would like to use the new concurrent collections in .NET 4.0 to solve the following problem.

The basic data structure I want to have is a producer consumer queue, there will be a single consumer and multiple producers.

There are items of type A,B,C,D,E that will be added to this queue. Items of type A,B,C are added to the queue in the normal manner and processed in order.

However items of type D or E can only exist in the queue zero or once. If one of these is to be added and there already exists another of the same type that has not yet been processed then this should update that other one in-place in the queue. The queue position would not change (i.e. would not go to the back of the queue) after the update.

Which .NET 4.0 classes would be best for this?

+2  A: 

I think there is no such (priority) queue in .net 4 that would support atomic AddOrUpdate operation. There only is ConcurrentDictionary that supports this, but it's not suitable if you need the order preserved.

So your option is maybe to use some combination of the two.

However, please be aware that you will lose the safety of the concurrent structures as soon as you do combined operations on them; you must implement the locking mechanism on your own (look here for an example of such situation: http://stackoverflow.com/questions/2678112/a-net4-gem-the-concurrentdictionary-tips-tricks).

Second option would be to google for some 3rd party implementations.

František Žiačik