Definition
Lock-free and wait-free are progress guarantees for a concurrent algorithm §14.1, one whose operations several threads (independent flows of execution that share one memory) can run at the same time. Lock-free means that whenever threads keep taking steps, some operation completes within a finite number of steps: the system as a whole always progresses. Wait-free means that every operation completes within a finite number of its own thread’s steps, whatever the other threads do. The difference that matters: under lock-freedom one thread can starve forever; under wait-freedom no thread can.
The idea
A lock makes progress depend on one thread. Suppose a thread holds a mutex §9.5, a lock that only one thread at a time may hold, and is then stopped: the scheduler gives the processor to another thread, or the thread crashes. Every other thread that needs the mutex now waits until the holder runs again. On a single-core microcontroller §6.1 the wait can become permanent. If an interrupt handler §6.12, a function the hardware runs in response to an external event, needs data that the interrupted thread has locked, the thread cannot run until the handler returns, and the handler cannot finish until the thread unlocks.
Lock-free and wait-free algorithms remove that dependence. They use no locks. They change shared data with atomic instructions such as compare-and-swap §9.9, which the hardware performs as one indivisible step: compare-and-swap writes a new value to a memory word only if the word still holds an expected old value, and reports whether it did. The algorithm is designed so that, whatever step a thread was stopped after, the other threads can still finish their own operations from the state that thread left behind.
The two guarantees differ in who is promised progress. When threads race to update a shared counter with compare-and-swap, one succeeds and the others fail and retry. Every failure is caused by another thread’s success, so the counter keeps advancing: that is lock-freedom. Yet the same thread may lose every race, so its own update may never finish. Wait-freedom closes that gap, usually by having the other threads complete the operation of a thread that falls behind.
How it works
The guarantees count steps, not time. A step is one action of one thread on shared memory, such as a read, a write or a compare-and-swap, together with the local computation before it. Four progress guarantees are standard for operations on shared data:
Wait-free: every operation finishes within a finite number of its own thread’s steps. Bounded wait-free adds a fixed limit on that number.
Lock-free: whenever threads keep taking steps, some operation finishes. A particular operation may never finish.
Obstruction-free: an operation finishes if its thread eventually runs alone, with every other thread paused, for long enough. Two threads that keep interfering with each other may both fail to finish.
Blocking, the class of every algorithm that uses locks: progress needs the lock holder to run. A deadlock-free lock guarantees that some waiting thread eventually acquires it, and a starvation-free lock that every waiting thread does, but both only on condition that the scheduler keeps running each holder until it unlocks.
The first three are called non-blocking, because each holds even if any number of other threads stop forever at any point. Every wait-free algorithm is lock-free, and every lock-free algorithm is obstruction-free.
A lock-free counter increment shows the usual structure:
Read the counter into a local variable .
Compute .
Compare-and-swap: if the counter still holds , replace it with and finish.
Otherwise another thread changed the counter after step 1; go back to step 1.
Step 3 fails only when another thread’s step 3 succeeded in between, so every failure is matched by a completed increment somewhere. That matching is the whole proof that the loop is lock-free. It sets no limit on how often one particular thread fails.
The same counter is wait-free on a processor with an atomic fetch-and-add instruction, which adds to a memory word and returns the old value in one step: every increment then takes exactly one step. The function atomic_fetch_add of C11, the 2011 edition of the C language standard [78], is called the same way on every processor. A processor without a fetch-and-add instruction runs it as a retry loop like the one above, for example with the load-exclusive and store-exclusive instructions of many Arm processors, which store only if no conflicting access to the word has been detected since the load; the counter is then only lock-free.
Some structures are wait-free with plain reads and writes. In a ring buffer §14.15 with exactly one producer thread and one consumer thread, a push reads two positions, checks for a full buffer, copies one element and publishes the new write position: a fixed number of steps with no retry. Acquire-release ordering §9.11 on the positions ensures that a consumer that sees a new position also sees the element written before it.
General wait-free algorithms use helping. Each thread announces its pending operation in a shared array §14.5 with one slot per thread and, before doing its own work, completes the announced operations of other threads, in an order that reaches every slot within a bounded number of rounds. Herlihy showed in 1991 that compare-and-swap is universal in this sense [296]: from it, every shared data structure has a wait-free implementation.
Worked example
Two threads add 1 to a counter that starts at 0, using the compare-and-swap loop above. Thread F is fast. Thread S is slow, for example because interrupts §6.11 keep stopping it. Consider this schedule:
S reads 0.
F reads 0, and its compare-and-swap replaces 0 with 1. F’s increment has completed.
S’s compare-and-swap expects 0, finds 1, and fails. S reads 1.
F reads 1 and replaces 1 with 2.
S’s compare-and-swap expects 1, finds 2, and fails. S reads 2.
Suppose the pattern of steps 2 and 3 repeats 1,000 times in all. The counter reaches 1,000. F has completed 1,000 increments; S has completed none and has failed 1,000 times. Lock-freedom holds: 1,000 operations completed, one for each failure of S. Wait-freedom fails: S’s single increment has already taken steps (1,001 reads and 1,000 failed compare-and-swaps), and nothing limits how many more it will take.
Now take a bounded wait-free operation, a push onto the ring buffer described above. Suppose an analysis of the code shows that one push takes at most 12 steps and that one step takes at most 4 processor cycles, on a processor that runs 64,000,000 cycles per second. One cycle lasts s, which is 15.625 ns. The push therefore needs at most cycles, which is ns of its own execution. That bounds execution time §9.32, not response time. If an interrupt arrives during the push and its handler runs for 20 microseconds, the push finishes at most microseconds after it began (750 ns is 0.75 microseconds). Wait-freedom bounds the steps; the time a thread spends not running comes from a separate scheduling analysis.
Stated precisely
Model an execution as a sequence of steps, each taken by one thread. Threads take steps only inside operations. An operation is pending from the step that starts it until the step that returns its result.
Wait-free: in every execution, every operation completes after a finite number of steps of its own thread, however the other threads’ steps are interleaved with them and whether or not the other threads stop.
Bounded wait-free: there is a number such that every operation completes within at most steps of its own thread. may depend on the number of threads ; if it does not, the algorithm is called population-oblivious.
Lock-free: no infinite execution exists in which only finitely many operations complete. Equivalently, from any point of any execution, if threads go on taking steps, some operation completes after finitely many of those steps.
Obstruction-free: every operation completes after a finite number of its own thread’s steps in any execution in which, from some point on, only that thread takes steps.
Each guarantee rests on three assumptions.
Indivisible, non-waiting steps. Every atomic instruction the algorithm uses is a single step that never waits for another thread. An atomic operation §9.8 that the platform emulates with a hidden lock makes the algorithm blocking.
Complete accounting. Every action inside the operation is counted, including memory allocation, the freeing of memory that other threads may still read, logging and library calls. One lock anywhere inside the operation makes it blocking.
Steps, not time. The guarantees count a thread’s own steps. They say nothing about how long the thread waits while the scheduler does not run it.
Limits and common mistakes
Lock-free does not mean starvation-free, as the worked example shows. Only wait-freedom, or a lock that serves waiters in arrival order on a scheduler that runs every thread, promises service to each thread.
Lock-free does not mean fast. Under heavy contention, threads waste work on failed compare-and-swaps, and the memory word they compete for moves between the caches of different cores, the small fast memories next to each core, on every attempt; a good lock can give higher throughput. Wait-free algorithms with helping usually cost more per operation than lock-free ones when there is no contention.
Wait-free does not mean bounded in time, and it does not mean one instruction. A step bound becomes a time bound only after adding the cost of each step and every interruption, every preemption (the scheduler switching to another thread) and every memory stall, as the worked example does.
No lock in the source code does not mean lock-free. A spinlock §9.7 built from atomic instructions is still a lock: if its holder stops, every other thread spins forever. The test is the definition: can a stopped thread prevent all others from completing operations? C11 provides atomic_is_lock_free and the ATOMIC_INT_LOCK_FREE family of macros, which report whether an atomic type is implemented without a hidden lock. A 64-bit atomic on a 32-bit processor without 64-bit atomic instructions may not be.
Freeing memory is part of the operation. A lock-free linked structure must not free a node that a stopped thread may still read. Hazard pointers, in which each thread publishes the nodes it is reading, keep operations lock-free with bounded memory. Epoch-based reclamation and read-copy update §9.20, which free a removed node only after every thread has been seen outside any operation since the removal, keep operations non-blocking, but memory waiting to be freed can grow without limit while one thread is stopped inside an operation. Reusing a node too early invites the ABA problem §9.25: a compare-and-swap succeeds because a value changed from A to B and back to A, although the data behind it changed.
A compare-and-swap built from load-exclusive and store-exclusive instructions can fail with no competing write; C11 allows this spurious failure for atomic_compare_exchange_weak. The counter’s lock-freedom then also depends on the hardware not failing one thread this way forever.
Questions answered
Can one thread starve in a lock-free algorithm?
Yes, one thread can starve in a lock-free algorithm. Lock-freedom promises only that some operation completes whenever threads keep running; it promises nothing to any particular thread. In a compare-and-swap retry loop, a slow thread can read the shared value, lose the compare-and-swap to a faster thread, retry, and lose again without end, while the faster thread completes an operation each time. The system progresses, so the algorithm is lock-free, but the slow thread’s operation never finishes. How often a thread loses depends on the timing of the others, and the guarantee sets no limit, so an argument about deadlines cannot rest on lock-freedom alone.
A wait-free algorithm rules starvation §9.18 out by bounding the steps of every operation, typically by having threads complete each other’s announced operations.
Related entries: Compare-and-swap (§9.9), Starvation (§9.18), Worst-case execution time (WCET) (§9.33), Atomic operation (§9.8), ABA problem (§9.25). Further reading: [78, 296].