Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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, 1212 runs 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.
  • program holds 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.
  • queries holds 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.
InstructionEffectPart
LOAD r xCopy shared variable x into register r1
STORE x vWrite the integer v into x1
STORE x rWrite the value of register r into x3
ADD r vAdd the integer v to register r; no shared variable is touched3
FADD r x vCopy x into r and set x to its old value plus v, as one step3

Steps

  • Each instruction is one indivisible step, like an atomic operation with memory_order_seq_cst, so a LOAD never sees half of a STORE.
  • FADD is fetch_add: it leaves the old value in r, and its read and write happen in one step, so no other thread can run between them.
  • Nothing else groups instructions together. A LOAD, an ADD, and a STORE written one after another can be split by other threads exactly as the three steps of ++counter were.

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, and FADD is a register, and so is the second operand of STORE when 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 STORE is 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
  • n is 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=2 comes before x=10, and x=-3 before x=0.

Part 1: Every interleaving and its outcome

  1. Run every interleaving of the program and print the report.
  2. 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 -100 and 100.

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.

  1. Each condition uses a name from the program, and no name repeats within a query.
  2. An outcome matches a query when every named value equals the required value. Names the query leaves out can hold anything.
  3. CAN asks whether at least one interleaving ends in a matching outcome. ALWAYS asks whether every interleaving does.
  4. After the report, print one answer line per query, in query order.
  5. 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.
QueryPrints
CANYES s, where s is the smallest schedule whose outcome matches, or NO when no outcome matches
ALWAYSYES 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=0 prints NO: 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=0 from 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_cst on all four operations forbids it, because only seq_cst promises 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:

  1. ADD r v changes only the thread's own register.
  2. STORE x r writes the register's current value.
  3. FADD r x v reads x, leaves that old value in r, and writes the old value plus v back to x, all in one step.
  4. 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=1 is the lost update: both threads loaded 0 before either stored.
  • With FADD r1 c 1 in thread 1 and FADD r2 c 1 in thread 2 instead, no interleaving can place one thread between the other's read and write.

Tests 7–10 cover this part.

Example
Input
2
STORE x 1; LOAD r1 y
STORE y 1; LOAD r2 x
0
Output
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
Explanation

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.