Concurrent Priority Queue in .NET 4.0

前端 未结 9 712
攒了一身酷
攒了一身酷 2020-12-09 02:32

It seems there are lots of improvements in .NET 4.0 related to concurrency that might rely on concurrent priority queues. Is there decent priority queue implementation insid

相关标签:
9条回答
  • 2020-12-09 02:57

    Check Thread-safe Collections in .NET Framework 4 and Their Performance Characteristics but AFAIK there are no ready to use priority queue. All new thread-safe collections doesn't maintain order but you can make your own on top of them. Check @Steven's way.

    0 讨论(0)
  • 2020-12-09 02:59

    I've found a great example of a concurrent priority queue here. Hope it will help you a little.

    var priorityQueue = new ConcurrentPriorityQueue<TKey, TValue>();
    

    TKey in the context of this queue could be an int value or any other object that implements IComparable.

    For consuming such a queue you may do a following:

    var priorityQueue = new ConcurrentPriorityQueue<int, object>(); 
    
    // Add elements
    priorityQueue.Enqueue(2, elementP2); 
    priorityQueue.Enqueue(1, elementP1);
    
    // Here you will receive elementP1
    bool result = priorityQueue.TryDequeue(out KeyValuePair<int, object> element);
    
    0 讨论(0)
  • 2020-12-09 03:00

    Well, 7 years passed, but for posterity, I would like to answer with my implementation.

    Documentation: Optionally awaitable simple to use Concurrent Priority Queue

    Sourcecodes: github

    nuget package

    • Lock-Free,
    • Highly Concurrent,
    • generic in stored item type,
    • generic in priority type, but constrained to priorities represented by .net enum, strongly typed priority,
    • explicitly defined descending order of priorities during construction,
    • ability to detect items count and per priority level items count,
    • ability to dequeue - descending order of priorities,
    • ability to override dequeue priority level,
    • potentially awaitable,
    • potentially priority based awaitable,
    0 讨论(0)
提交回复
热议问题