liveBook · Manning

Sequenced-before relation

As explained in C++ Concurrency in Action, Second Edition

A C++ ordering relation between evaluations within a single thread.

The sequenced-before relation orders evaluations within one thread. Operations in successive statements are ordered, while operations within one statement are generally unsequenced unless a language rule, such as the built-in comma operator, establishes an order. section 5.3.2

Listing 5.3. Order of evaluation of arguments to a function call is unspecified
#include <iostream>
void foo(int a,int b)
{
    std::cout<<a<<","<<b<<std::endl;
}
int get_num()
{
    static int i=0;
    return ++i;
}
int main()
{
    foo(get_num(),get_num());   #1
}

The two calls to get_num() are function arguments whose evaluation order is unspecified, so the output may be 1,2 or 2,1. section 5.3.2

Sequenced-before combines with inter-thread happens-before: if an operation precedes a release operation, and that release synchronizes with an acquire in another thread, the earlier operation happens before accesses after that acquire. This ordering supports safe access to data protected by mutexes and underlies higher-level synchronization facilities. section 5.3.7 section 5.3.2

How C++ Concurrency in Action, Second Edition covers it

How deep it goes
Explains statement and expression ordering, then traces its composition with synchronization and acquire-release operations.
Examples
Uses function arguments with unspecified evaluation order and a spinlock protecting shared data.
What it assumes
Assumes familiarity with ordinary single-thread sequencing while explaining how synchronization extends its consequences across threads.
What you can do after
Evaluate when operations are ordered within a thread and how that order carries across a synchronization boundary.
How it connects
Connects to happens-before, inter-thread happens-before, synchronizes-with, acquire-release operations, and mutexes.

Also explained in

Compare how 2 books explain sequenced-before relation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of C++ Concurrency in Action, Second Edition. The section links point to the parts of the book it draws on.