ConcurrentPriorityQueueT 0.7.1
See the version list below for details.
dotnet add package ConcurrentPriorityQueueT --version 0.7.1
NuGet\Install-Package ConcurrentPriorityQueueT -Version 0.7.1
<PackageReference Include="ConcurrentPriorityQueueT" Version="0.7.1" />
paket add ConcurrentPriorityQueueT --version 0.7.1
#r "nuget: ConcurrentPriorityQueueT, 0.7.1"
// Install ConcurrentPriorityQueueT as a Cake Addin
#addin nuget:?package=ConcurrentPriorityQueueT&version=0.7.1
// Install ConcurrentPriorityQueueT as a Cake Tool
#tool nuget:?package=ConcurrentPriorityQueueT&version=0.7.1
ConcurrentPriorityQueueT
A concurrent priority queue with parallelism.
Nuget Package: https://www.nuget.org/packages/ConcurrentPriorityQueueT/
Features
- Fully concurrent, All operations are thread-safe.
- Parallel enqueuing (threads do not lock the entire collection when enqueuing).
- Specifying the type of priority queue (Min or Max).
- Specifying if duplicates are allowed on enqueuing.
- Utilizes the asynchronous programming model (APM) to invoke callbacks for asynchronous methods.
Complexity
Enqueue: O(n), Calling thread must wait until after iterating the list for correct placement.<br /> EnqueueAsync: O(n), Calling thread immediately continues after offloading the work onto a worker thread.<br /> Dequeue: O(1), Calling thread must wait if the queue is empty but active enqueues are present.<br /> DequeueAsync: O(1), Calling thread immediately continues after offloading the work onto a worker thread.<br /> Peek: O(1), Calling thread must wait if the queue is empty but active enqueues are present.<br />
Usage:
Properties
/// <summary>
/// The priority mode to set the priority queue.
/// </summary>
public PriorityType Mode { get; private set; }
/// <summary>
/// The number of values currently in the queue.
/// </summary>
public int Count
/// <summary>
/// The number of asynchronous operations currently scheduled.
/// </summary>
public int AsyncEnqueueOperations
Constructor
/// <summary>
/// Creates a new concurrent priority queue.
/// </summary>
/// <param name="mode">The type of priority to use for the queue.</param>
public ConcurrentPriorityQueue(PriorityType mode = PriorityType.Max)
Methods
/// <summary>
/// Tries to add an entry to the priority queue.
/// </summary>
/// <param name="priority">A comparable value to be used as the priority.</param>
/// <param name="value">The value to be stored into the queue.</param>
/// <param name="allowDuplicates">Whether or not to allow adding if an entry with the same priority already exists.</param>
/// <returns>true if entry was successfully added to the queue, otherwise false.</returns>
/// <remarks>This method blocks until the enqueue operation has completed.</remarks>
public bool TryEnqueue(P priority, V value, bool allowDuplicates = true)
/// <summary>
/// Asynchronously adds to the priority queue.
/// </summary>
/// <param name="priority">A comparable value to be used as the priority.</param>
/// <param name="value">The value to be stored into the queue.</param>
/// <param name="callback">The callback to invoke once the enqueue operation has completed.</param>
/// <remarks>This method does not block.</remarks>
public void TryEnqueueAsync(P priority, V value, Action<AsyncPriorityOperationResult>? callback = null)
/// <summary>
/// Asynchronously adds an entry to the priority queue.
/// </summary>
/// <param name="priority">A comparable value to be used as the priority.</param>
/// <param name="value">The value to be stored into the queue.</param>
/// <param name="allowDuplicates">Whether or not to allow adding if an entry with the same priority already exists.</param>
/// <param name="callback">The callback to invoke once the enqueue operation has completed.</param>
/// <remarks>This method does not block.</remarks>
public void TryEnqueueAsync(P priority, V value, bool allowDuplicates, Action<AsyncPriorityOperationResult>? callback = null)
/// <summary>
/// Tries to retrieve and remove from the priority queue.
/// </summary>
/// <param name="value">The value which has been dequeued.</param>
/// <returns>true if an entry has been successfully dequeued, otherwise false.</returns>
/// <remarks>This method blocks until the dequeue operation has completed.</remarks>
public bool TryDequeue(out V value)
/// <summary>
/// Asyncronously dequeues an entry from the priority queue.
/// </summary>
/// <param name="callback">The callback delegate to invoke after dequeuing has completed.</param>
/// <remarks>This method does not block.</remarks>
public void TryDequeueAsync(Action<AsyncPriorityOperationResult>? callback = null)
/// <summary>
/// Tries to retrieve without removing from the priority queue.
/// </summary>
/// <param name="value">The value at the beginning of the priority queue.</param>
/// <returns>true if an entry has been successfully retrieved, otherwise false.</returns>
/// <remarks>This method blocks until the peek operation has completed.</remarks>
public bool TryPeek(out V? value)
/// <summary>
/// Blocks until all asynchronous enqueue opertions are completed.
/// </summary>
/// <remarks>This method blocks until there are no more active asynchronous enqueues</remarks>
public void WaitForAsyncEnqueues(int millisecondsTimeout = 0)
/// <summary>
/// Clears the entries currently in the queue.
/// </summary>
/// <remarks>Asynchronous enqueues after this method will still be added.</remarks>
public void Clear()
Examples:
The data type of P in ConcurrentPriorityQueue<P, V> must implement IComparable<P>
using System.Collections.Concurrent;
// Create a min type priority queue where 'int' is used for the priority and 'string' is used as the value type
ConcurrentPriorityQueue<int, string> priorityQ = new(PriorityType.Min);
Synchronous operations can be made with the TryEnqueue() and TryDequeue() methods.
// Enqueue synchronously disallowing duplicates
if (priorityQ.TryEnqueue(1, "World", false)) Console.WriteLine("Enqueue success"); else Console.WriteLine("Duplicate entry");
if (priorityQ.TryEnqueue(1, "World", false)) Console.WriteLine("Enqueue success"); else Console.WriteLine("Duplicate entry");
if (priorityQ.TryEnqueue(0, "Hello", false)) Console.WriteLine("Enqueue success"); else Console.WriteLine("Duplicate entry");
Console.WriteLine();
// Dequeue synchronously
if (priorityQ.TryDequeue(out string value1)) Console.WriteLine(value1); else Console.WriteLine("Queue is empty");
if (priorityQ.TryDequeue(out string value2)) Console.WriteLine(value2); else Console.WriteLine("Queue is empty");
if (priorityQ.TryDequeue(out string value3)) Console.WriteLine(value3); else Console.WriteLine("Queue is empty");
Asynchronous operations can be made with the TyEnqueueAsync() and TryDequeueAsync() methods.
// Enqueue asynchronously
priorityQ.TryEnqueueAsync(0, "Hello World", false, r =>
{
if (r)
Console.WriteLine($"Enqueued: {r.Value}");
else
Console.WriteLine("Enqueue dup");
});
priorityQ.TryEnqueueAsync(0, "Hello World", false, r =>
{
if (r)
Console.WriteLine($"Enqueued: {r.Value}");
else
Console.WriteLine("Enqueue dup");
});
Console.WriteLine();
// Dequeue asynchronously
priorityQ.TryDequeueAsync(r =>
{
if (r)
Console.WriteLine($"Dequeued: {r.Value}");
else
Console.WriteLine("Queue is empty");
});
priorityQ.TryDequeueAsync(r =>
{
if (r)
Console.WriteLine($"Dequeued: {r.Value}");
else
Console.WriteLine("Queue is empty");
});
Enqueues can run in parallel.
// Parallel
Random rnd = new();
Parallel.For(0, 10000, i =>
{
int n = rnd.Next(0, 100000);
priorityQ.TryEnqueueAsync(n, n.ToString());
});
// Waits for all asynchronous enqueues to complete with a 2 second timeout
// Initially checks and if async enqueues are complete, wait 2 seconds and checks again
priorityQ.WaitForAsyncEnqueues(2000);
foreach (string str in priorityQ)
Console.WriteLine(str);
Console.WriteLine($"Entries: {priorityQ.Count}");
await Task.Delay(-1);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.