The idea: take a number and wait for your turn

A ticket lock works like the number machine at a deli counter. The lock is one word with two counters: next_ticket and now_serving. To lock, a thread runs fetch-and-add on next_ticket: one atomic step that returns the old value and adds 1. The old value is the thread's ticket. The thread then spins until now_serving equals its ticket. To unlock, the holder adds 1 to now_serving, and the thread with the next ticket goes in.

The lock word holds next_ticket = 4 and now_serving = 1. T2 has ticket 0 and has left, T1 has ticket 1 and is inside, T4 with ticket 2 and T3 with ticket 3 wait for their turn.
Every thread gets a different number from fetch_add, and the lock is handed on in number order.
lock:    my = fetch_add(&next_ticket, 1);
         while (now_serving != my)
             ;                          /* spin */
unlock:  now_serving = now_serving + 1;

A plain spinlock such as the xchg spinlock or test-and-set has no queue: when the lock comes free, whoever gets there first wins. The ticket lock adds a queue without any list, just two numbers. That gives it FIFO fairness: no thread can be passed over.

How to use the animation

Four threads T1 to T4 run on CPUs 0 to 3. CPUs 0 and 1 sit on socket 0, CPUs 2 and 3 on socket 1. Every thread loops: work, take the lock, stay inside for 2 ticks, unlock. Pick the lock and the threads' workload, then press Step Tick to see one tick in detail or Run to End for all 24 ticks. The timeline at the bottom shows what each thread did in each tick: W work, S spin or wait, C critical section, P preempted. The table counts how often each thread got the lock, its longest wait, and how many times a later thread went in before it (overtaken).

When several threads try an atomic in the same tick, they run one after another. The order comes from a seeded schedule: each try gets a random delay, plus one extra unit when the cache line sits on the other socket, and the smallest delay goes first. The same demo always gives the same run.

Fairness: ticket lock vs test-and-set

Both demos use the same four threads, each working 1 tick between turns. With the ticket lock every thread got the lock 3 times, nobody was overtaken, and the longest wait was 6 ticks: three other critical sections of 2 ticks each (Demo 3). That bound holds for any run: with n threads, a waiter waits for at most n − 1 critical sections.

With test-and-set, the thread that releases the lock owns the cache line, and its neighbour on the same socket gets the line fastest. So T1 and T2 passed the lock back and forth. T3 tried in all 24 ticks and never got in; it was overtaken 9 times. T4 got in once, after 22 ticks (Demo 4). This is starvation: the lock works, but one thread makes no progress.

Two timelines of 24 ticks. Ticket lock: T1 to T4 each get the lock 3 times in turn. Test-and-set: T1 gets it 5 times and T2 6 times, T3 spins in all 24 ticks and gets it 0 times, T4 gets it once at tick 24.
Same threads, same ticks: the ticket lock serves everyone in turn, test-and-set lets the nearest CPUs win again and again.
Short work, 24 ticksGot the lock (T1, T2, T3, T4)Longest waitOvertaken
Ticket lock3, 3, 3, 36 ticks0 times
Test-and-set lock5, 6, 0, 124 ticks (T3)9 times (T3 and T4)

Cache-line traffic: everyone spins on one word

Each CPU keeps a copy of the lock's cache line. While nothing changes, a spinner reads its own copy (state S) and costs nothing. But every write to the line turns all other copies invalid (I). The ticket lock writes the line twice per turn: the fetch_add of a new arrival and the release. After each write, every spinner misses and fetches the line again, although only one of them can go in.

In Demo 2, T2 releases with T1, T3 and T4 spinning. T2's store costs 1 transfer, and the three re-reads cost 3 more: 4 transfers for one release. With n waiters a release costs n + 1. On a machine with 64 waiting cores that is 65 transfers, all crossing the interconnect, to hand the lock to one core. Test-and-set is no better: each failed test_and_set is a write, so its spinners move the line even while the lock is held (Demo 4 needed 66 transfers against 60 for the ticket lock). Why a shared line bounces between cores is shown in MESI cache coherence and false sharing.

When the next waiter is preempted

Strict order has a cost. In Demo 5 T4 holds the next ticket but loses its CPU in ticks 4 to 9. When T1 unlocks in tick 5, it is T4's turn, and nobody else may go in. The lock stays free for 5 ticks while T3, then T2 and T1, spin. T3's longest wait grows to 11 ticks. With test-and-set the free lock simply goes to someone else, so it is never idle while a thread waits (Demo 6), but T4 then waits up to 12 ticks with no turn reserved. This is why ticket locks behave badly in virtual machines, where a vCPU holding the next ticket can be descheduled; Linux added paravirtual spinlocks to handle it.

The next step: MCS queue locks

The MCS lock (Mellor-Crummey and Scott, 1991) keeps the fairness and removes the broadcast. Each waiter brings its own queue node and spins on a flag in that node, which sits in its own cache line. The lock word only holds a pointer to the last node (the tail). To lock, a thread swaps itself into the tail and links itself behind the old tail. To unlock, the holder writes the flag of the next node only. One release now costs about 2 transfers, however many threads wait.

Left: T2 writes now_serving and the line goes to T1, T3 and T4, 4 transfers. Right: MCS queue T2, T1, T3, T4 with tail on T4; T2 writes only T1's flag, 2 transfers.
A ticket lock's release wakes every spinner; an MCS release touches only the next waiter's line.
LockFair (FIFO)Transfers per release, n waitersMemory
Test-and-setNon + 1, plus traffic while held1 word
Ticket lockYesn + 11 word
MCS lockYesabout 21 word + 1 node per waiter

Linux used ticket spinlocks on x86 from 2008 (kernel 2.6.25). Since kernel 4.2 it uses qspinlock, a 4-byte lock that queues waiters MCS-style. Java's ReentrantLock takes another route: waiters in a queue are parked, not spinning, as shown in ReentrantLock and AQS.

See also Mutex vs Spinlock for when spinning is worth it at all, and Compare-and-Swap for the other common atomic.

What the page leaves out

  • Real timing: a tick here stands for anything from one atomic instruction to a whole critical section. Real cache misses take tens to hundreds of nanoseconds, more across sockets.
  • The arbitration rule is a toy: real interconnects do not give a fixed bonus to the near socket, but near CPUs do win more often, which is why NUMA-aware locks exist (see NUMA).
  • Proportional back-off (wait about my − now_serving times a delay before reading again) and the pause instruction in the spin loop.
  • Counter wrap-around: the counters are small integers that overflow, which is fine as long as fewer threads wait than the counter can count.
  • Memory ordering: the acquire and release barriers that keep the critical section between lock and unlock.
  • MCS details: the race when the holder unlocks while a new waiter is still linking itself in, CLH locks, and qspinlock's pending bit.

On the mind map

This page on the Concurrency mind map · 29 pages in 7 branches · open it to see the whole map and test yourself in recall mode.