TL;DR
A linearizable shared object is safe to use from several threads, but safety says nothing about how long a call takes. A wait-free object guarantees that every operation finishes in a bounded number of its own steps, no matter how slowly the other threads run, and even if some of them stop forever. This matters because a lock held by a paused thread blocks everyone else, and a retry loop can make one thread lose again and again.
Builds on: Linearizability, Mutual Exclusion, Thread, Compare-and-Swap · Leads to: the bivalence argument, which shows that some objects cannot be wait-free
Where it's used
Real-time audio. A sound card asks for the next block of samples at a fixed rate. A block of 512 samples at 48,000 samples per second is due every 512 / 48,000 seconds, about 10.7 milliseconds. If the audio thread waits on a lock that a lower-priority thread holds, and that thread gets paused by the operating system, the audio glitches. Audio engines therefore pass samples through lock-free or wait-free ring buffers.
Interrupt handlers and kernel code. An interrupt handler in the Operating System runs on top of whatever code was interrupted. If that code holds a lock, the handler cannot wait for it, because the lock holder cannot run until the handler returns. Shared state touched in interrupt context has to be updated without blocking, which is a wait-free or lock-free requirement.
Atomic counters in standard libraries. The java.util.concurrent.atomic package and C++'s std::atomic give programmers single shared values, such as counters and flags, that many threads can update without a mutex. A counter whose increment is one hardware instruction is wait-free, since the instruction completes in one step.
How it works
We will use one shared counter c, which starts at 0, and see what each design guarantees when several threads add 1 to it.
-
The lock-based version. A thread takes the lock, reads
c, writesc + 1, and releases the lock. The other threads wait at the lock. Check: if the lock holder is paused for one second, every other thread makes no progress during that second. Its number of steps is not bounded by anything the waiting thread controls. -
The compare-and-swap idea. Hardware offers an instruction
CAS(c, e, v): it writesvintoconly ifcstill holds the expected valuee, and it reports whether it wrote. A thread can increment without a lock: readcto gete, computee + 1, thenCAS(c, e, e + 1). Check: ifcwas 0 and the CAS was called withe = 0, the write happens and the CAS reports success. -
The retry loop. When two threads both read 0 in a race, the first CAS succeeds and the second one fails, because
cis now 1 and not 0. The loser reads again and tries again. Check: the loser's CAS fails exactly when another thread's write got in between its read and its CAS. -
Why the retry loop is not wait-free. Nothing limits how many times the loser can fail. If the other thread keeps finishing its increments between this thread's read and CAS, this thread never succeeds. The system as a whole still makes progress, because someone's CAS keeps succeeding, but this one thread has no bound on its steps. Check: a loop that fails three times in a row has taken three CAS steps and still has not finished.
-
The one-step version. Some hardware offers a fetch-and-add instruction, which adds a value to
cand returns the old value, all in one indivisible step. Each thread calls it once. Check: three calls on a counter at 0 return 0, 1 and 2 in some order, and leavecat 3. Each call is a single step. -
Naming the guarantees. An operation is wait-free if its thread finishes within a fixed number of its own steps. It is lock-free if, whatever the schedule, some operation always finishes. It is obstruction-free if an operation finishes when its thread runs alone. Wait-free implies lock-free, and lock-free implies obstruction-free. Check: the retry loop in step 3 is lock-free but not wait-free.
flowchart TD A["Read the current value"] --> B["Compute the new value"] B --> C["Try compare-and-swap: write only if still unchanged"] C -- "succeeded" --> D["Done"] C -- "failed, someone else wrote first" --> A
The math
A shared object is a data structure that several threads call through operations. An execution is a sequence of steps, each step taken by one thread. A thread's own steps are the steps in the execution that that thread takes.
Step 1 in symbols: the lock-based bound
With a lock, a waiting thread's step count is not bounded by , because its steps are spent in a loop waiting for the lock holder. Write for the number of steps thread spends waiting. If the lock holder is paused, can grow without limit, so the object is not wait-free. A blocking object is one where a stopped thread can stop everyone else.
Step 2 in symbols: the CAS primitive
A CAS-based increment repeats two steps, a read and a CAS, until the CAS returns true. Each failed CAS means some other thread's write changed the value, so the system as a whole made progress.
Step 3 in symbols: the progress hierarchy
Let the execution be infinite.
- Lock-free: infinitely many operations return.
- Wait-free: each operation returns after at most of its thread's steps.
A wait-free object is lock-free. The converse is false: the retry loop has infinitely many completions overall, but a single thread can fail forever.
Step 4 in symbols: consensus numbers
Each thread proposes a value. A wait-free consensus protocol for threads requires that every thread returns the same value, that this value was proposed by some thread, and that every thread returns after a bounded number of its own steps. The consensus number of a primitive is the largest for which together with ordinary read/write registers can solve wait-free consensus for threads.
- Read/write registers: .
- Fetch-and-add: .
- Compare-and-swap: .
A primitive with a consensus number of cannot build a wait-free object that needs threads to agree.
Worked examples
Shared counter . Threads , , each call fetch-and-add with 1 once.
- runs its single step: it receives the old value 0 and sets .
- runs its single step: it receives 1 and sets .
- runs its single step: it receives 2 and sets .
- Each thread took exactly one step, so each operation is wait-free with .
- The three returned values are 0, 1 and 2, which are distinct, and the final value is 3.
Interpretation: a single hardware instruction gives a wait-free counter, and every thread gets a different ticket number no matter the schedule.
Shared counter . Threads and each want to add 1 with a CAS loop. Count only each thread's own steps.
- reads and gets . This is 's step 1.
- reads and gets . This is 's step 1.
- calls . Since , the write happens and . returns after its second step.
- calls . Since , nothing is written, and the CAS returns false. This is 's second step.
- reads again and gets 1. This is 's third step.
- calls . Since , the write happens and . This is 's fourth step, and returns.
- Final value . used 4 steps, and used 2.
Interpretation: 's cost grew from 2 steps to 4 because got in first. If had kept winning every round, 's count would have no upper bound, so this loop is lock-free but not wait-free.
Try it: in example 2, suppose wins every round, so each time tries its CAS, has already changed. How many steps does need before it returns? Answer: there is no fixed number. Each round costs two more steps and still ends in failure, so 's count is unbounded, which is exactly why this loop is not wait-free.
An audio producer writes samples into a ring buffer of slots, and the audio callback reads them. The writer's index and the reader's index both count modulo 8. Keep one slot empty to tell "full" from "empty", so the usable capacity is . Here and .
- The number of stored samples is .
- , and (add 8 to get 5).
- So 5 samples are stored, and the writer can add more before the buffer is full.
- A write reads , checks for space, writes one slot, and moves forward. A read does the same in reverse. Each is a fixed number of loads and stores with no loop and no lock, so each is wait-free for one producer and one consumer.
- Compare with the deadline: a 512-sample block at 48,000 samples per second is due every 10.7 milliseconds. A wait-free operation adds a bounded delay, so the callback can meet that deadline even if the producer thread is descheduled.
Interpretation: the buffer's arithmetic is trivial, but the property that matters is that neither side waits for the other.
Try it: with , and , how many samples are stored, and how many more can the writer add? Answer: samples are stored, and the writer can add more.
Common mistakes and tradeoffs
It is tempting to think that "lock-free" means "has no locks." In fact it is a progress guarantee: some operation always finishes, whether or not a lock appears in the code. A lock-free design can still use memory barriers and retry loops, and a design with no visible lock can still block if one thread waits on another. The guarantee is what matters, not the mechanism.
It is tempting to think that a CAS loop is wait-free because it usually succeeds on the first try. In fact the guarantee is about the worst case. Under heavy contention a retry loop can fail for a long time, and a lock-free algorithm bounds the system's progress, not any one thread's.
It is tempting to think that wait-free means "never retries." In fact a wait-free algorithm may retry, provided each thread's retries are bounded. Many wait-free constructions use "helping," where a thread that succeeds finishes the pending operations of threads that have not, which bounds how long any one thread can be delayed.
When to use it. Choose wait-free designs when a thread has a deadline or when a thread may be interrupted, paused or killed while it holds shared state: audio, interrupt handlers, real-time control, and recovery code.
When not to use it. For ordinary server code with low contention, a plain mutex is usually simpler, easier to reason about and faster. Wait-free constructions are often hard to get right, use more memory, and can be slower on average.
Alternatives. Keep critical sections short with a lock, use lock-free data structures where a retry loop is acceptable, or use read-copy-update for data that is read far more often than it is written.
History
Programmers wrote lock-free and blocking shared structures case by case for decades, with no common vocabulary for what each one guaranteed. In 1991 Maurice Herlihy formalised wait-freedom and introduced consensus numbers, which rank synchronization primitives by how many threads they can coordinate. The ranking showed that registers alone cannot solve consensus even for two threads, while compare-and-swap can handle any number of threads. Herlihy also showed that CAS can build a wait-free version of any object, a result that became known as universality and is closely related to State Machine Replication. The idea caught on because it turned a vague goal into a measurable one and told hardware designers which primitives were worth building.
Recap
- A shared object is safe when its operations are linearizable, but safety does not bound how long an operation takes.
- A lock lets a paused thread block everyone, and a CAS retry loop lets one thread lose forever.
- Wait-free means each operation finishes within a bound on its own steps. Lock-free means the system always makes progress. Wait-free implies lock-free.
- Consensus numbers rank primitives: registers have 1, fetch-and-add 2, and compare-and-swap is unbounded.
- Next, read Bivalence, which uses the same state-based reasoning to show that some tasks cannot be solved in certain settings.