Lesson 6 of 9 · 27m
Lock-Free Data Structures Without the Folklore
What lock-free actually guarantees, why memory reclamation and ABA are the real hard parts, and when a bounded queue or mutex is the senior-engineer answer.
Lock-free programming has an undeserved glamour problem. Engineers hear "no locks" and imagine scalability for free. The truth is narrower: a lock-free algorithm guarantees that some thread makes progress in a bounded number of steps, even if other threads pause. That progress guarantee matters in kernels, allocators, low-latency systems, and interrupt-like contexts. In ordinary application code, the complexity often costs more than it saves.
Lock-free is a progress guarantee, not a performance label
The standard hierarchy is worth knowing:
- blocking — a paused thread can stop everyone behind the lock
- lock-free — some thread always makes progress
- wait-free — every thread makes progress in a bounded number of its own steps A lock-free queue can still be slow. A mutex-protected queue can still outperform it. The reason to choose lock-free is usually a progress requirement or a measured contention bottleneck, not aesthetics.
The Treiber stack shows the pattern and the danger
template <class T>
class TreiberStack {
struct Node {
T value;
Node* next;
};
std::atomic<Node*> head{nullptr};
public:
void push(T value) {
Node* node = new Node{std::move(value), head.load()};
while (!head.compare_exchange_weak(node->next, node)) {
// retry with the updated head written into node->next
}
}
};Push is conceptually simple: prepare a node, then swing the head pointer if nobody else moved it. Pop is harder because after reading head, another thread may pop and free the node before you dereference it. Deleting memory at the wrong time is the central problem of lock-free data structures.
Memory reclamation is the hard part
You cannot free a node while another thread might still hold a pointer to it. Production solutions include:
- hazard pointers — each thread publishes pointers it is actively using; reclamation checks those hazard lists
- epoch-based reclamation — retire memory and reclaim it after every thread has passed through a quiescent state
- reference counting — possible, but naive atomic reference counting often costs enough to erase the benefit
This is why mature libraries matter. Crossbeam in Rust, Java's
java.util.concurrent, and battle-tested C++ libraries implement the reclamation details most application teams should not hand-roll.
ABA is a value equality trap
CAS compares values, not history. A pointer can change from A to B and back to A; CAS sees "still A" even though the structure changed in between. In pointer-heavy algorithms this can resurrect deleted nodes or corrupt links. Common mitigations are tagged pointers with a version counter, wider double-word CAS where available, or reclamation schemes that prevent reuse while observers exist.
Bounded queues beat cleverness in most services
A fixed-capacity multi-producer/multi-consumer queue is one of the few lock-free structures worth adopting widely because it makes backpressure explicit:
producer -> bounded queue -> fixed worker poolWhen full, the producer blocks, drops, or applies pressure upstream — a deliberate decision. An unbounded lock-free queue can hide overload until memory exhaustion. In services, boundedness is often more important than lock-freedom.
Field notes
- If you cannot explain memory reclamation, you are not done designing the lock-free structure.
- Measure against a mutex baseline. Lock-free code with cache-line ping-pong often loses.
- Exponential backoff reduces CAS contention but can worsen tail latency; tune it as a latency/throughput tradeoff.
- Do not mix ad-hoc locks and lock-free operations on the same fields. The synchronization domains must be coherent.
- Copy-on-write snapshots are often the practical alternative: cheap readers, serialized writers, simple reclamation.
Was this lesson helpful?