Section 9.12

Sequential consistency

Definition

Sequential consistency is a memory consistency model: a rule that says which values the reads of a multithreaded program may return. In Leslie Lamport’s 1979 definition [295], the result of any execution is the same as if the operations of all processors, or threads, ran in some single sequential order, in which each processor’s operations appear in the order its program specifies. Each read returns the latest write to its location in that order. Threads may interleave in any way, but no thread’s operations are reordered.

The idea

Programmers naturally reason about threads, instruction sequences that share one memory, by interleaving: take one step from this thread, then one from that thread, as if a single processor ran them all and switched between them. Sequential consistency is the promise that this reasoning is valid. Most people assume it without noticing.

Hardware and compilers do not provide it for free. A processor core puts its writes into a store buffer, a queue of writes not yet visible to other cores, and runs later loads before those writes leave the buffer. A compiler moves loads and stores that look independent within one thread. Both reorderings are invisible to a thread running alone, and both break the interleaving picture for several threads. Languages therefore promise sequential consistency only to programs that follow a rule.

Sequential consistency orders individual reads and writes. It says nothing about which interleaving happens, so it never glues several operations into one. A check followed by an update is two operations, and another thread’s operation may fall between them.

How it works

To decide whether an outcome is possible under sequential consistency:

  1. List each thread’s memory operations in program order.

  2. Form every interleaving: every merge of those lists into one list that keeps each thread’s operations in their own order.

  3. Run each interleaving against a single memory. Each read returns the value of the last write to the same location earlier in the list, or the initial value if there is none.

  4. The outcomes of these runs are exactly the outcomes that sequential consistency allows.

Two threads of mm and nn operations have (m+nm)\binom{m+n}{m} interleavings: the number of ways to choose which mm of the m+nm + n positions belong to the first thread.

In C11 and C++11, the 2011 editions of the C and C++ standards [78, 247], an atomic operation §9.8 that names no memory order uses memory_order_seq_cst. The standard requires one total order SS of all memory_order_seq_cst operations, consistent with the happens-before relation §9.10, in which each such load reads the last such store to its location before it, unless it reads a write made with a weaker order. A note in the C11 standard states the guarantee that results: a program that uses only mutexes §9.5 and sequentially consistent atomic operations to prevent every data race §9.3 behaves as though its threads’ operations were simply interleaved. This guarantee is called sequential consistency for data-race-free programs (SC for DRF).

To keep that promise, compilers emit barriers. On x86 processors a sequentially consistent store becomes an xchg instruction, or a store followed by mfence, because x86 otherwise lets a later load overtake an earlier store. On Armv8-A processors the load-acquire and store-release instructions are already strong enough for sequentially consistent loads and stores.

Linearizability, which Herlihy and Wing defined in 1990, carries the same idea from single reads and writes to whole operations on a shared object, such as taking a ticket, and adds a real-time rule: if one operation finishes before another begins, it must come first in the order. Making a check-and-update atomic means making it one linearizable operation with a single linearization point §8.6, the instant at which it takes effect.

Worked example

One ticket remains. An atomic integer available holds 1, and every access to it is sequentially consistent. Threads A and B each run: load available; if it is 1, store 0 and issue a ticket.

  1. A loads available and gets 1.

  2. B loads available and gets 1.

  3. A stores 0 and issues a ticket.

  4. B stores 0 and issues a ticket.

This order keeps A’s load before A’s store and B’s load before B’s store, so sequential consistency allows it, and each load returned the latest write before it, the initial 1. Yet two tickets were issued when only one remained. Of the (42)=6\binom{4}{2} = 6 interleavings, the 4 in which both loads come before both stores issue two tickets. Sequential consistency is fully respected; the program is wrong because another thread’s load fell between a check and its update.

The repair makes the check and the update one operation. With compare-and-swap §9.9 (CAS), an atomic step that writes a new value only if the location still holds an expected value, each thread runs CAS(𝚊𝚟𝚊𝚒𝚕𝚊𝚋𝚕𝚎,1,0)\mathrm{CAS}(\texttt{available}, 1, 0) and issues a ticket only on success. Whichever CAS comes first in the order finds 1, writes 0 and succeeds. The second finds 0, which is not the expected 1, and fails. Exactly one ticket is issued in every interleaving. A mutex held across the load, the test and the store gives the same result. Strengthening the memory order of the separate load and store cannot, because they are already sequentially consistent.

Sequential consistency does forbid some outcomes that real hardware produces. Let x and y start at 0. Thread A stores 1 to x and then loads y into r1r_1; thread B stores 1 to y and then loads x into r2r_2. The (42)=6\binom{4}{2} = 6 interleavings give:

No interleaving gives r1=r2=0r_1 = r_2 = 0. For r1=0r_1 = 0, A’s load must come before B’s store; for r2=0r_2 = 0, B’s load must come before A’s store; and each thread’s store comes before its own load. Together these form a cycle, A’s store, A’s load, B’s store, B’s load, A’s store, which no single order contains. With ordinary store and load instructions an x86 processor does produce r1=r2=0r_1 = r_2 = 0, because each store can wait in its core’s store buffer while the later load runs. Declaring x and y as C11 atomic objects with the default order makes the compiler add the barriers that rule it out.

Stated precisely

Let an execution consist of the memory operations of threads T1,…,TkT_1, \ldots, T_k, and let program order be each thread’s own order of its operations. The execution is sequentially consistent if a total order TT of all its memory operations exists such that:

  1. TT contains program order: if aa comes before bb in one thread’s program order, aa comes before bb in TT;

  2. every read rr of a location xx returns the value of the last write to xx that precedes rr in TT, or the initial value of xx if no write to xx precedes rr.

A system is sequentially consistent if every execution it allows is.

The model constrains returned values only. It does not choose which order TT occurs. It does not require an operation that finished earlier by a clock to come earlier in TT; linearizability adds that rule. It treats no group of operations as one: an operation made of several reads and writes is atomic only if the program prevents every conflicting operation of another thread from falling between them.

In C11 the guarantee holds for programs that synchronize only through mutexes and memory_order_seq_cst atomic operations and that contain no data race. Any weaker order removes the promise for the operations it interacts with, and a data race gives the program undefined behavior §4.12.

Limits and common mistakes

Sequential consistency does not make compound operations atomic. A check followed by an action, a read followed by a write, or an update of two variables can be split by another thread’s operations in a perfectly legal interleaving.

It is not a progress guarantee. It promises nothing about fairness, bounded waiting or which thread runs; a sequentially consistent spin loop can still wait forever.

It does not remove data races. C promises it only to programs without them, and a non-atomic variable shared without synchronization still gives undefined behavior.

It is not linearizability. Sequential consistency allows an order in which an operation that finished earlier by a clock comes later; linearizability forbids that.

It costs speed. Sequentially consistent stores need a full barrier on many processors, x86 and Armv7 among them, so many lock-free §9.13 algorithms §14.1 use acquire-release ordering §9.11 where it suffices. Programs that mix orders on the same variables must be checked against the standard’s rules, not against the interleaving picture.

It covers neither object lifetime §4.1 nor devices. Freeing memory too early, and the order in which a device sees writes to its memory-mapped registers, need their own rules.

Questions answered

Does sequential consistency make a multi-operation check-and-update atomic?

No, sequential consistency does not make a multi-operation check-and-update atomic. It orders individual memory operations, and any interleaving of them is still allowed, including one in which another thread’s operation falls between the check and the update. Sequential consistency is the memory model in which every execution behaves as if all threads’ reads and writes ran one at a time in a single order that keeps each thread’s own order, with each read returning the latest write before it. The check (a load) and the update (a store) are two operations in that order, and nothing forces them to be adjacent.

Example: with one ticket left (available holds 1), threads A and B can both load 1, then both store 0, and both issue a ticket; that interleaving is sequentially consistent. To make the check-and-update atomic, make it one operation: a single atomic read-modify-write, such as a compare-and-swap from 1 to 0 that only one thread can win, or a mutex held across the whole check and update. Strengthening the memory order of the separate load and store cannot help, because the problem is the gap between two operations, not the order in which their effects become visible.

Related entries: Atomic operation (§9.8), Compare-and-swap (§9.9), Linearization point (§8.6), Race condition (§9.2), Acquire-release ordering (§9.11). Further reading: [78, 85, 247, 295].