//--- Constructors --- /// <summary> /// Create a new instance of the queue. /// </summary> /// <param name="maxPriority">Maximum priority for <see cref="TryEnqueue"/>.</param> public LockFreePriorityQueue(int maxPriority) { _queues = new LockFreeQueue <T> [maxPriority + 1]; for (int i = 0; i < _queues.Length; i++) { _queues[i] = new LockFreeQueue <T>(); } }
/// <summary> /// Try to get an item from the queue. /// </summary> /// <param name="priority">Storage location for priority of the removed item.</param> /// <param name="item">Storage location for the item to be removed.</param> /// <returns><see langword="True"/> if the dequeue succeeded.</returns> public bool TryDequeue(out int priority, out T item) { for (int i = 0; i < _queues.Length; ++i) { LockFreeQueue <T> queue = _queues[i]; if (queue.TryDequeue(out item)) { priority = i; return(true); } } priority = -1; item = default(T); return(false); }