C# ConcurrentStack: Thread-Safe LIFO Collection
Learn how to use C# ConcurrentStack for thread-safe LIFO operations: Push, TryPop, TryPeek, bulk methods, and the performance tradeoffs to consider.

ConcurrentStack<T> is a thread-safe last-in, first-out (LIFO) collection in the System.Collections.Concurrent namespace. It lets multiple threads push and pop items without external locking. This article covers its core methods, bulk operations, thread-safety limitations, and how it compares with other collections.
Unlike a regular Stack<T>, ConcurrentStack<T> uses lock-free operations internally to coordinate concurrent access, so you do not need an explicit lock around individual calls. That design has tradeoffs, and the sections below explain when those tradeoffs matter.
What ConcurrentStack Provides
ConcurrentStack<T> implements the classic stack semantics: the last item pushed is the first item popped. Multiple threads can call Push, TryPop, or TryPeek concurrently without corrupting the collection. This makes it useful in LIFO work-stealing strategies, undo/redo systems, or producer-consumer patterns where the newest item should be processed first.
The collection uses a linked list of nodes internally. Each Push allocates a new node, and operations update the head pointer with atomic compare-and-exchange instructions. This design avoids locks but adds per-item allocation overhead, which is a key difference from array-based collections.
Core Operations: Push, TryPop, and TryPeek
The primary methods are Push, TryPop, and TryPeek. Push adds an item to the top of the stack. TryPop removes the top item and returns it through an out parameter, returning false if the stack is empty. TryPeek returns the top item without removing it and also returns false if the stack is empty.
var stack = new ConcurrentStack<int>();
stack.Push(1);
stack.Push(2);
if (stack.TryPop(out int result))
{
Console.WriteLine(result); // Output: 2
}
if (stack.TryPeek(out int top))
{
Console.WriteLine(top); // Output: 1
}The Try* pattern is important because checking Count before a pop introduces a race condition. Between the check and the pop, another thread could empty the stack. TryPop handles that check and removal atomically, so use it instead of a manual Count check.
Bulk Operations: PushRange and TryPopRange
When you need to add or remove multiple items at once, PushRange and TryPopRange are more efficient than calling Push or TryPop in a loop. PushRange takes an array and pushes all its elements onto the stack. TryPopRange fills a destination array with up to its length of items and returns the number actually popped.
var stack = new ConcurrentStack<int>();
var items = new[] { 1, 2, 3 };
stack.PushRange(items);
var buffer = new int[3];
int popped = stack.TryPopRange(buffer);
// popped == 3, buffer contains { 3, 2, 1 }TryPopRange pops items in LIFO order, so the first element of the destination array receives the top of the stack. The return value is the number of items popped, which may be less than the buffer length if the stack runs out. Always inspect the return value so you know how many elements are valid.
Thread-Safety Guarantees and Limitations
Individual operations on ConcurrentStack<T> are atomic and thread-safe. A Push or TryPop is safe to call from multiple threads without adding synchronization around that call. Compound operations are not automatically atomic. For example, checking whether the stack is non-empty and then popping based on that check is unsafe because another thread could pop the last item in between. Always use TryPop and handle the false return case.
Enumeration of a ConcurrentStack is weakly consistent. It does not provide a snapshot of the stack at a single point in time, and items added or removed during enumeration may or may not appear. If you need a consistent snapshot, copy the stack first using ToArray or CopyTo.
ConcurrentStack<T> does not have a Clear method. To reset it, create a new instance and replace the existing reference, or repeatedly call TryPop until it returns false. The TryPop loop is not atomic and can interleave with other producers or consumers, so creating a new instance is usually the safer approach.
Performance Considerations
ConcurrentStack<T> is lock-free, so individual operations do not take an OS lock. The algorithm still relies on atomic operations and retries, so many threads pushing or popping at the same time can create contention on the head pointer. In high-contention cases, throughput can be lower than with a carefully designed lock-based approach, but the lock-free design avoids some locking overhead and may scale better than a single global lock.
Because the stack is a linked list, each Push allocates a new node. Pushing very large numbers of items can create memory pressure and garbage-collection overhead. An array-based Stack<T> protected by a lock may be a better choice for low-contention scenarios or when minimizing allocations matters.
ConcurrentQueue<T> is the FIFO alternative. It is also lock-free, but its internal storage and allocation behavior are different from ConcurrentStack's per-item linked nodes. Choose ConcurrentStack when you need LIFO; choose ConcurrentQueue when items should be processed in FIFO order.
Choosing Between ConcurrentStack and Alternatives
The decision to use ConcurrentStack<T> depends on ordering and concurrency requirements. The following table compares common options:
| Collection | Ordering | Thread-safe | Best Use Case |
|---|---|---|---|
| ConcurrentStack<T> | LIFO | Yes | Work-stealing, undo/redo, last-in-first-out processing |
| ConcurrentQueue<T> | FIFO | Yes | Producer-consumer, first-in-first-out processing |
| Stack<T> with lock | LIFO | Yes (with lock) | Low contention, minimal allocation overhead |
| BlockingCollection<T> | Configurable | Yes | Producer-consumer with blocking and bounded capacity |
Use ConcurrentStack when you need LIFO ordering and multiple threads will access the collection concurrently. If you need FIFO, use ConcurrentQueue. If contention is low and you want to avoid per-item allocation, a plain Stack<T> protected by a lock might be simpler and use less memory. BlockingCollection adds producer-consumer features such as blocking and cancellation, which are useful when you need to wait for items to become available.
Common Mistakes and Edge Cases
One common mistake is relying on the Count property to decide whether to pop. Count can change immediately after you read it. Always use TryPop and handle the false case gracefully, typically by retrying or exiting a loop.
Another edge case is TryPopRange with a partially filled buffer. If the stack has fewer items than the buffer length, the method returns the actual count, and you must process only the first popped elements.
When using PushRange, the array is pushed as a group, but individual items are still in LIFO order within the stack. For example, pushing {1, 2, 3} puts 3 on top, then 2, then 1. If you need the opposite order, push the array in reverse.
Finally, avoid using ConcurrentStack<T> as a replacement for a regular Stack<T> in single-threaded code. The lock-free operations and per-item node allocation add unnecessary overhead. Measure your actual concurrency requirements before introducing a concurrent collection.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.