liveBook · Manning

Happens-before relation

As explained in Modern C

A happened-before relation is an ordering relation that establishes that one evaluation or event precedes another for purposes of reasoning about concurrent execution.

The happens-before relation orders evaluations and events so concurrent execution can be analyzed as a coherent whole. In C terms, F happens before E, written F → E; the relation is observed after the events occur. section 19.1

TAKEAWAY

  • Sequenced-before evaluations in one thread also establish a happens-before relation. section 19.1
  • Synchronization between threads comes from atomic operations or certain C library calls. section 19.1
  • Atomic modifications have a modification order consistent with the sequenced-before relations of threads accessing that atomic object. section 19.1

From one thread to two

If an assignment to command[0] is sequenced before a switch statement, every case is executed after that assignment, or is perceived as happening later; passing command to nested calls therefore provides the modified value. Across threads, an atomic write followed by another thread's read of the written value supplies synchronization. section 19.1 section 19.1

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.

A figure traces two threads synchronized through atomic object x, showing its modifications and each thread's view of state. section 19.0

How Modern C covers it

How deep it goes
Defines the relation, connects it to sequenced-before and synchronization, and stops short of dependency-ordered-before details.
Examples
Uses a command assignment before a switch and a two-thread atomic object with ordered modifications.
What it assumes
Assumes familiarity with evaluations, threads, atomics, and sequenced-before; situates them within C's execution model.
What you can do after
Evaluate concurrent execution by identifying sequenced-before relationships and synchronization events.
How it connects
Connects happens-before with sequenced-before, atomic operations, modification order, synchronization, and sequential consistency.

Also explained in

Compare how 5 books explain happens-before relation →

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.