Interleavings Under Sequential Consistency
Prerequisites: Atomics and Memory Ordering Intro, Threads, Shared State, and Data Races
memory_order_seq_cst, the default order of every std::atomic operation, promises that a program behaves as if all its atomic operations ran in one global order that every thread agrees on.
Take that promise literally. Given a few tiny threads, run every global order it allows and report which final values the threads can produce.
- Program order: the order a thread's instructions are written in.
- Interleaving: the merged order of the threads' steps, from Threads, Shared State, and Data Races. Here it must keep each thread's own instructions in program order.
- Sequential consistency (SC): every run of the program is some interleaving. Instructions execute one at a time, each finishing before the next begins, and every thread sees their effects in that one order.
- Litmus test: a tiny program used this way, together with a question about which final values it can produce.
- Schedule: an interleaving written as the thread numbers in execution order, with no spaces. For two threads of two instructions each,
1212runs thread 1's first instruction, then thread 2's first, then thread 1's second, then thread 2's second. - Outcome: the final value of every name once all threads have finished. Different interleavings can end in the same outcome.
What to implement
explore_interleavings(program, queries)returns the lines of a report.programholds one line per thread: the first line is thread 1, the second is thread 2, and so on.- Each line lists that thread's instructions in program order, separated by semicolons, as in
STORE x 1; LOAD r1 y. queriesholds 0 to 20 lines, which Part 2 explains.- On stdin: the number of program lines, the program lines, the number of queries, then the queries.
- There are three parts. One solution must pass all eleven tests.
| Instruction | Effect | Part |
|---|---|---|
LOAD r x | Copy shared variable x into register r | 1 |
STORE x v | Write the integer v into x | 1 |
STORE x r | Write the value of register r into x | 3 |
ADD r v | Add the integer v to register r; no shared variable is touched | 3 |
FADD r x v | Copy x into r and set x to its old value plus v, as one step | 3 |
Steps
- Each instruction is one indivisible step, like an atomic operation with
memory_order_seq_cst, so aLOADnever sees half of aSTORE. FADDisfetch_add: it leaves the old value inr, and its read and write happen in one step, so no other thread can run between them.- Nothing else groups instructions together. A
LOAD, anADD, and aSTOREwritten one after another can be split by other threads exactly as the three steps of++counterwere.
Names and operands
- Operands are names and integers. A name is 1 to 8 lowercase letters and digits, starting with a letter.
- A name is either a shared variable, which every thread can read and write, or a register, which belongs to one thread, like the on-chip slots the CPU does its arithmetic in.
- The first operand of
LOAD,ADD, andFADDis a register, and so is the second operand ofSTOREwhen it is a name. Every other name operand is a shared variable. - No name is used both ways, and each register appears in only one thread.
- The second operand of
STOREis an integer exactly when it starts with a digit or-. - Every shared variable and every register starts at 0.
Report format
INTERLEAVINGS n
name=value name=value ... : count
nis the number of interleavings. One line per distinct outcome follows.- An outcome line lists every name in the program, registers and shared variables together, sorted by plain string comparison. Each is written
name=value, separated by single spaces. - After the last pair come a space, a colon, a space, and the number of interleavings that end in that outcome, as in
r1=0 r2=1 x=1 y=1 : 1. - Outcome lines are sorted by their values as integers, name by name in printed order: the first name's value decides, the second breaks a tie, and so on. So
x=2comes beforex=10, andx=-3beforex=0.
Part 1: Every interleaving and its outcome
- Run every interleaving of the program and print the report.
- The visiting order does not change the report, because the outcome lines are sorted.
Limits
- 1 to 4 threads, each with 1 to 4 instructions and at most 10 instructions in all, so no program has more than 25,200 interleavings.
- Every integer in an instruction is between
-100and100.
Tests 1–3 and 11 cover this part. Their programs use only LOAD and STORE with an integer, and they have no queries.
Part 2: Litmus questions
Each query is CAN or ALWAYS followed by one or more conditions of the form name=value.
- Each condition uses a name from the program, and no name repeats within a query.
- An outcome matches a query when every named value equals the required value. Names the query leaves out can hold anything.
CANasks whether at least one interleaving ends in a matching outcome.ALWAYSasks whether every interleaving does.- After the report, print one answer line per query, in query order.
- Each answer names the smallest schedule that proves it. All schedules of one program have the same length, so compare them as strings: the smaller schedule has the smaller thread number at the first position where the two differ.
| Query | Prints |
|---|---|
CAN | YES s, where s is the smallest schedule whose outcome matches, or NO when no outcome matches |
ALWAYS | YES when every outcome matches, or NO s, where s is the smallest schedule whose outcome does not match |
One widely used litmus test is store buffering, the program in the example below.
- Under SC,
CAN r1=0 r2=0printsNO: whichever store comes first in the interleaving, the other thread's load runs after it and reads 1. - Real CPUs can produce
r1=0 r2=0from plain loads and stores. An x86 core, for example, keeps a store buffer, a small queue that holds the core's recent stores before other cores can see them.- The core's next load can read memory while its own store is still waiting in that queue. If both cores do this at the same time, both loads read 0.
- The C++ standard allows the same outcome when the four operations use
memory_order_relaxed, and also when the stores are release stores and the loads are acquire loads. - Only
memory_order_seq_cston all four operations forbids it, because onlyseq_cstpromises the single global order that this problem enumerates.
Tests 4–6 cover this part. Test 5 is the publish pattern from Atomics and Memory Ordering Intro, with data standing for the update and flag for ready.
Part 3: Increments and fetch_add
Add the other three instructions from the table:
ADD r vchanges only the thread's own register.STORE x rwrites the register's current value.FADD r x vreadsx, leaves that old value inr, and writes the old value plusvback tox, all in one step.- Values can become negative. The canonical order still compares them as integers.
Two threads that each run LOAD r1 c; ADD r1 1; STORE c r1, with r2 in thread 2, are the racing counter from Threads, Shared State, and Data Races.
- An outcome with
c=1is the lost update: both threads loaded 0 before either stored. - With
FADD r1 c 1in thread 1 andFADD r2 c 1in thread 2 instead, no interleaving can place one thread between the other's read and write.
Tests 7–10 cover this part.
2 STORE x 1; LOAD r1 y STORE y 1; LOAD r2 x 0
INTERLEAVINGS 6 r1=0 r2=1 x=1 y=1 : 1 r1=1 r2=0 x=1 y=1 : 1 r1=1 r2=1 x=1 y=1 : 4
Each thread has two instructions, so there are 4! / (2! * 2!) = 6 interleavings, and there are no queries. The names in sorted order are r1, r2, x, y.
In schedule 1122, thread 1 runs both of its instructions first: x becomes 1, and r1 reads y while it is still 0. Thread 2 then sets y to 1 and reads x = 1 into r2, giving r1=0 r2=1.
Schedule 2211 is the mirror image and gives r1=1 r2=0. In the other four schedules (1212, 1221, 2112, 2121), both stores run before either load, so both registers read 1.
Both shared variables end at 1 in every interleaving, and no interleaving ends with r1=0 r2=0.