开发者

.net 4.0 concurrent queue dictionary

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 开发者_StackOverflow中文版update.

Which .NET 4.0 classes would be best for this?


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: A .Net4 Gem: The ConcurrentDictionary - Tips & Tricks).

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜