ConcurrentQueue<T>
What it is
ConcurrentQueue<T> is a FIFO (first-in-first-out) queue from the System.Collections.Concurrent namespace, designed to be used safely from multiple threads without explicit locking (lock).
var queue = new ConcurrentQueue<string>();
queue.Enqueue("pierwszy");
queue.Enqueue("drugi");
if (queue.TryDequeue(out var item))
{
Console.WriteLine(item); // "pierwszy"
}
How it differs from Queue<T> + lock
A plain Queue<T> is not thread-safe — if two threads call Enqueue/Dequeue at the same time, the internal structure can get corrupted. The classic solution is your own lock:
private readonly Queue<string> _queue = new();
private readonly object _lock = new();
public void Enqueue(string item)
{
lock (_lock) { _queue.Enqueue(item); }
}
public bool TryDequeue(out string? item)
{
lock (_lock) { return _queue.TryDequeue(out item); }
}
This works, but lock means that only one thread at a time can perform an operation — the others wait (block), which under heavy load creates context-switching overhead (a thread being put to sleep and resumed by the operating system).
ConcurrentQueue<T> is lock-free — internally it uses the processor's atomic operations (Interlocked.CompareExchange and similar) to modify the structure safely without blocking all access. In practice many threads can try at the same time, and those that "fail" at a given moment simply retry the operation (spin + retry), instead of waiting in a queue for a monitor as with lock. For high-contention scenarios this is usually noticeably faster.
Why TryDequeue/TryPeek and not Dequeue/Peek
ConcurrentQueue<T> does not have the ordinary Dequeue()/Peek() methods that throw an exception when the collection is empty — it has only the Try* variants:
bool TryDequeue(out T result);
bool TryPeek(out T result);
The reason: in a multithreaded environment, checking "is the collection empty" (IsEmpty) and the retrieval of an element that follows are two separate operations. Between them another thread can add or take an element — a classic race condition (TOCTOU — "time-of-check to time-of-use"). The Try* pattern combines "check and take" into one atomic operation, eliminating that window for error.
// WRONG - a race between IsEmpty and Dequeue
if (!queue.IsEmpty)
{
var item = queue.Dequeue(); // tej metody nawet nie ma, i dobrze
}
// DOBRZE - atomowo
if (queue.TryDequeue(out var item))
{
Use(item);
}
Example: producer-consumer (a background log processor)
A typical use case: many threads add tasks/logs to the queue (producers), and one background thread reads and processes them (the consumer) — without blocking the producers.
public class BackgroundLogProcessor
{
private readonly ConcurrentQueue<string> _logQueue = new();
private readonly CancellationTokenSource _cts = new();
public void Enqueue(string message) => _logQueue.Enqueue(message);
public Task StartProcessingAsync()
{
return Task.Run(async () =>
{
while (!_cts.Token.IsCancellationRequested)
{
if (_logQueue.TryDequeue(out var message))
{
await WriteToFileAsync(message);
}
else
{
await Task.Delay(50); // nothing to do - wait a moment
}
}
});
}
private Task WriteToFileAsync(string message) =>
File.AppendAllTextAsync("log.txt", message + "\n");
public void Stop() => _cts.Cancel();
}
Any number of threads can call Enqueue at the same time (e.g. from different HTTP requests), and one background thread calmly consumes them — without lock, without the risk of blocking the producer threads.
What about Channel<T>?
A newer, often better tool for the producer-consumer pattern is System.Threading.Channels.Channel<T> (since .NET Core 3.0):
ConcurrentQueue<T>— a synchronous data structure; if the consumer wants to "wait" for a new element without polling (Task.Delayin a loop), it has to implement that itself.Channel<T>— has native async support (await reader.ReadAsync(), which genuinely suspends until an element appears, with no polling) and backpressure (limiting the buffer size, blocking/dropping the producer when it is full).
In new code, Channel<T> is often chosen for producer-consumer communication; ConcurrentQueue<T> still works great as a simple, shared buffer when there is no need to natively await new elements.
The System.Collections.Concurrent family
| Type | Structure | Non-thread-safe equivalent |
|---|---|---|
ConcurrentQueue<T> |
FIFO | Queue<T> |
ConcurrentStack<T> |
LIFO | Stack<T> |
ConcurrentDictionary<TKey, TValue> |
a key-value map | Dictionary<TKey, TValue> |
ConcurrentBag<T> |
an unordered set, optimised for the same thread adding and taking | — |
They are all lock-free (or nearly so — ConcurrentDictionary uses fine-grained internal locks in some operations), and they all expose the Try* pattern for the same reason as ConcurrentQueue<T>.