Inter-thread happens-before orders operations across threads. If an operation in one thread synchronizes with an operation in another, the first happens-before the second; the relation is transitive and can combine with sequenced-before ordering within either thread. section 5.3.2
Ordering matters when threads access the same memory location:
- Conflicting accesses—where at least one is a write—need defined ordering to avoid a data race and its undefined behavior. section 5.1.2
- A shared mutex or atomic-operation synchronization can establish ordering; with multiple threads, every pair of accesses needs defined ordering. section 5.1.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 function-call example illustrates a sequencing caveat: two get_num() calls used as arguments have unspecified evaluation order, distinguishing within-statement sequencing from inter-thread synchronization. section 5.3.2
