liveBook · Manning

Consistency model

As explained in Modern C

A C memory model is the language-level model that specifies how objects occupy storage and how their locations can be accessed during execution.

Imagine two threads sharing an object. One changes it; the other needs to know when that change is visible. A consistency model supplies rules for reasoning about that visibility. In C, timing between threads is expressed through the happens-before relation, established by synchronization using atomic objects or specific library functions. section 19.0 section 19.5

Figure 19.1. Two threads that synchronize via an atomic. The circles present the modifications of object x. The bars below the threads represent information about the state of A, and those above represent information about the state of B.

Follow the two threads and the modifications of the shared object. The bars depict what each thread knows about the other's state; synchronization is what makes cross-thread reasoning possible. section 19.0

TAKEAWAY: Sequential consistency is the default consistency model for atomics, not for other C library functions. It additionally assumes a total order of the corresponding synchronization events. That assumption can be expensive; explicit acquire-release consistency can be more efficient, but requires careful arguments to atomic functions with an _explicit suffix. section 19.5

Do not confuse this question with C's object-and-storage memory model. That model treats objects as byte representations accessible through typed pointers; it also constrains aliasing. A consistency model addresses how concurrent parts of an execution can reason about changes to shared state. section 12.0 section 12.3

How Modern C covers it

How deep it goes
Compares sequential and acquire-release consistency and their costs, while setting aside consume consistency and its dependency ordering.
Examples
Uses a two-thread diagram showing modifications to a shared object and information about each thread's state.
What it assumes
Assumes prior exposure to atomic objects and threads, then revisits visibility across concurrent execution.
What you can do after
Evaluate/understand cross-thread timing and why explicit acquire-release operations need careful design.
How it connects
Links happens-before, synchronization, atomic objects, threads, and C library synchronization functions.

Also explained in

Compare how 4 books explain consistency model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.