the c++ execution model - github pages · c++ programs describe operations that are performed on...
TRANSCRIPT
![Page 1: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/1.jpg)
The C++ Execution Model
Bryce Adelstein Lelbach
@blelbach
Core C++ 2019
1
![Page 2: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/2.jpg)
Copyright (C) 2018 NVIDIA Corporation
The Abstract Machine
The C++ abstract machine is a portable abstraction of
your operating system, kernel and hardware.
2
![Page 3: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/3.jpg)
Copyright (C) 2018 NVIDIA Corporation
The Abstract Machine
The C++ abstract machine is a portable abstraction of
your operating system, kernel and hardware.
The abstract machine is the intermediary between your
C++ program and the system that it is run on.
3
![Page 4: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/4.jpg)
Copyright (C) 2018 NVIDIA Corporation
The Abstract Machine
int main(){std::thread t(f);
// ...}
4
![Page 5: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/5.jpg)
Copyright (C) 2018 NVIDIA Corporation
The Abstract Machine
Source: anandtech.com, intel.com, cs.cmu.edu, slideshare.net
Windows OS
Job
Process
Primary Thread
Fiber Fiber
Thread
Process
5
![Page 6: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/6.jpg)
Copyright (C) 2018 NVIDIA Corporation
The Abstract Machine
Storage
Threads
Abstract MachinePortable Code Concrete Systems
6
Windows OS
Job
Process
Primary Thread
Fiber Fiber
Thread
Process
int main(){
std::thread t(f);
// ...}
![Page 7: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/7.jpg)
Copyright (C) 2018 NVIDIA Corporation
The Abstract Machine
C++ programs describe operations that are performed
on the abstract machine.
C++ implementations define the characteristics of the
abstract machine and translate operations on the
abstract machine into operations on the system.
7
![Page 8: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/8.jpg)
Copyright (C) 2018 NVIDIA Corporation
The Abstract Machine
Storage
Threads
8
![Page 9: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/9.jpg)
Copyright (C) 2018 NVIDIA Corporation
Storage Model
Storage is flat; no notion hierarchy (caches, etc).
Objects reside in storage at a single memory location.
[intro.object] p9
9
![Page 10: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/10.jpg)
Copyright (C) 2018 NVIDIA Corporation
Storage Model
Some objects may not have a unique memory location.
Eligible empty base classes.
Objects marked [[no_unique_address]].
[intro.object] p7
10
![Page 11: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/11.jpg)
Copyright (C) 2018 NVIDIA Corporation
Storage Model
An implementation is allowed to store two objects at
the same machine address or not store an object at all.
[basic.memobj] footnote 32
11
![Page 12: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/12.jpg)
Copyright (C) 2018 NVIDIA Corporation
Storage Model
An object cannot have more than one memory location.
12
![Page 13: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/13.jpg)
Copyright (C) 2018 NVIDIA Corporation
Storage Model
struct A {X x;
X& X::operator=(X const& rhs){
if (&rhs == this) return *this;auto newx = new X(*rhs.x);delete x;x = newx;return *this;
}};
13
![Page 14: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/14.jpg)
Copyright (C) 2018 NVIDIA Corporation
Storage Model
Every thread in a program can potentially access every
object and function in a program.[intro.multithread] p1 s2
14
![Page 15: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/15.jpg)
Copyright (C) 2018 NVIDIA Corporation
Storage Model
Process 0
Main Memory
PU0 PU1L1 L2 L1
Virtual Memory
Process
Memory
15
![Page 16: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/16.jpg)
Copyright (C) 2018 NVIDIA Corporation
Storage Model
Process 0
Main Memory
PU0 PU1L1 L2 L1
Virtual Memory
Process
Memory
16
![Page 17: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/17.jpg)
Copyright (C) 2018 NVIDIA Corporation
Storage Model
Process 0
Process
Memory
Process 1
Process
Memory
Shared
Memory
Main Memory
PU0 PU1L1 L2 L1
Virtual Memory
17
![Page 18: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/18.jpg)
Copyright (C) 2018 NVIDIA Corporation
Storage Model
Process 0
Process
Memory
Process 1
Process
Memory
Shared
Memory
Main Memory
PU0 PU1L1 L2 L1
Virtual Memory
18
![Page 19: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/19.jpg)
Copyright (C) 2018 NVIDIA Corporation
Storage Model
Process 0
PU0 Memory
PU0 PU1
Virtual Memory
Process
Memory
PU1 Memory
19
![Page 20: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/20.jpg)
Copyright (C) 2018 NVIDIA Corporation
Storage Model
Process 0
PU0 Memory
PU0 PU1
Virtual Memory
Process
Memory
PU1 Memory
20
![Page 21: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/21.jpg)
Copyright (C) 2018 NVIDIA Corporation
Threads of Execution
A thread of execution is a single flow of control in a
program which evaluates a function call; threads may
run concurrently.[intro.multithread] p1 s1
21
![Page 22: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/22.jpg)
Copyright (C) 2018 NVIDIA Corporation
Threads of Execution
A thread of execution is a single flow of control in a
program which evaluates a function call; threads may
run concurrently.[intro.multithread] p1 s1
Main Thread of Execution
Evaluate main()
22
![Page 23: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/23.jpg)
Copyright (C) 2018 NVIDIA Corporation
Threads of Execution
A thread of execution is a single flow of control in a
program which evaluates a function call; threads may
run concurrently.[intro.multithread] p1 s1
Main Thread of Execution
Evaluate main()
std::thread t(f);
Evaluate f()
23
![Page 24: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/24.jpg)
Copyright (C) 2018 NVIDIA Corporation
Threads of Execution
Variables with static storage duration are initialized as
a consequence of program initiation. Variables with
thread storage duration are initialized as a
consequence of thread execution.[basic.start.static] p1
Main Thread of Execution
Static storage initialization
Evaluate main()
Static storage destruction
std::thread t(f);
Evaluate f()
24
![Page 25: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/25.jpg)
Copyright (C) 2018 NVIDIA Corporation
Threads of Execution
Variables with static storage duration are initialized as
a consequence of program initiation. Variables with
thread storage duration are initialized as a
consequence of thread execution.[basic.start.static] p1
Main Thread of Execution
Static storage initialization
Thread storage initialization
Evaluate main()Thread storage destruction
Static storage destruction
std::thread t(f);
Thread storage initialization
Evaluate f()Thread storage destruction
25
![Page 26: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/26.jpg)
Copyright (C) 2018 NVIDIA Corporation
Threads of Execution
Okay, so threads evaluate a function call.
What does it mean to evaluate a function call?
26
![Page 27: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/27.jpg)
Copyright (C) 2018 NVIDIA Corporation
Expressions
An expression is a sequence of operators and operands
that specifies a computation.
[expr.pre] p1 s2
f();f(a, b);
a + b;// `operator+(a, b)` call.
T a = 2;T a(2);// `T::T(int)` call.
27
![Page 28: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/28.jpg)
Copyright (C) 2018 NVIDIA Corporation
Expressions
Subexpressions are a part of a larger expression.
[intro.execution] p3, p4
T a = (*f)(a + b, c);
T::T call
(*f) a + b c
(*f)(a + b, c);
28
![Page 29: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/29.jpg)
Copyright (C) 2018 NVIDIA Corporation
Expressions
Full expressions are not subexpressions.
[intro.execution] p5
if (a == T()) { … }// Full expression includes:// lvalue-to-rvalue conversion// T-to-bool conversion// operator==(T, T) call
29
![Page 30: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/30.jpg)
Copyright (C) 2018 NVIDIA Corporation
Expressions
Full expressions are not subexpressions.
[intro.execution] p5
if (a == T()) { … }// Full expression includes:// lvalue-to-rvalue conversion// T-to-bool conversion// operator==(T, T) call
{T b;
} // Full expression: T::~T
30
![Page 31: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/31.jpg)
Copyright (C) 2018 NVIDIA Corporation
Expressions
Full expressions may include subexpressions that are not
lexically part of the expression.
[intro.execution] p6
void f(T a = g());
f();// Full expression includes:// g call// T::T call// f call
31
![Page 32: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/32.jpg)
Copyright (C) 2018 NVIDIA Corporation
Evaluations
Evaluation of an expression includes value computations
and the initiation of side effects.
[intro.execution] p7 s2
32
![Page 33: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/33.jpg)
Copyright (C) 2018 NVIDIA Corporation
Evaluations
Side effects change the environment:
33
![Page 34: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/34.jpg)
Copyright (C) 2018 NVIDIA Corporation
Evaluations
Side effects change the environment:
Reading a volatile object or modifying any object.
34
![Page 35: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/35.jpg)
Copyright (C) 2018 NVIDIA Corporation
Evaluations
Side effects change the environment:
Reading a volatile object or modifying any object.
Calling a library I/O function.
35
![Page 36: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/36.jpg)
Copyright (C) 2018 NVIDIA Corporation
Evaluations
Side effects change the environment:
Reading a volatile object or modifying any object.
Calling a library I/O function.
Calling a function that does any of the above.
[intro.execution] p7 s1
36
![Page 37: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/37.jpg)
Copyright (C) 2018 NVIDIA Corporation
Evaluations
int a;int b;a = a + b;
cout << a * a;
foo(a + b);
37
![Page 38: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/38.jpg)
Copyright (C) 2018 NVIDIA Corporation
Evaluations
Value computations are pure and have no observable
effect.
[intro.execution] p7 s2
38
![Page 39: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/39.jpg)
Copyright (C) 2018 NVIDIA Corporation
Evaluations
Completion of the execution of an evaluation does not
imply completion of its side effects.
[intro.execution] p7 s3
39
![Page 40: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/40.jpg)
Copyright (C) 2018 NVIDIA Corporation
Evaluations
cout << a * a;
40
![Page 41: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/41.jpg)
Copyright (C) 2018 NVIDIA Corporation
Sequenced Before
Given any two evaluations A and B within the same
thread of execution, if A is sequenced before B, then
the execution of A shall precede the execution of B.
[intro.execution] p8 s2
41
![Page 42: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/42.jpg)
Copyright (C) 2018 NVIDIA Corporation
Sequenced Before
The sequenced before relationship is…
Asymmetric: A is sequenced before B does not
imply that B is sequenced before A.
[intro.execution] p8 s1
Transitive: If A is sequenced before B and B is
sequenced before C, then A is sequenced before C.
[intro.execution] p8 s1
42
![Page 43: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/43.jpg)
Copyright (C) 2018 NVIDIA Corporation
Sequenced Before
Each full expression is sequenced before the next full
expression in program order.
[intro.execution] p9
a;b;// a sequenced before b
43
![Page 44: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/44.jpg)
Copyright (C) 2018 NVIDIA Corporation
Sequenced Before
If A and B are indeterminately sequenced, then either A
is sequenced before B or B is sequenced before A, but
it is unspecified which. E.g. A and B are not
interleaved.
[intro.execution] p8 s4
44
![Page 45: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/45.jpg)
Copyright (C) 2018 NVIDIA Corporation
Sequenced Before
If A and B are unsequenced, then A is not sequenced
before B and B is not sequenced before A. E.g. A and B
may be interleaved.
[intro.execution] p8 s3
45
![Page 46: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/46.jpg)
Copyright (C) 2018 NVIDIA Corporation
Sequenced Before
a
b
a
b
b
a
a
b
b
a
a
b
A is sequenced before B
A and B are
indeterminately sequenced
A and B are
unsequenced
b
a
46
![Page 47: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/47.jpg)
Copyright (C) 2018 NVIDIA Corporation
Sequenced Before
constexpr float a = // ...std::vector<float> x = // ...std::vector<float> y = // ...
std::for_loop(std::execution::seq,0, x.size(),[&] (int i) {y[i] += a * x[i];
});
load y[i]
load x[i]
fma a * x[i] + y[i]
store y[i]
load y[i+1]
load x[i+1]
fma a * x[i+1] + y[i+1]
store y[i+1]
load y[i+2]
load x[i+2]
fma a * x[i+2] + y[i+2]
store y[i+2]
load y[i+3]
load x[i+3]
fma a * x[i+3] + y[i+2]
store y[i+3]
47
![Page 48: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/48.jpg)
Copyright (C) 2018 NVIDIA Corporation
Sequenced Before
constexpr float a = // ...std::vector<float> x = // ...std::vector<float> y = // ...
std::for_loop(std::execution::unseq,0, x.size(),[&] (int i) {y[i] += a * x[i];
});
load y[i]
load y[i+1]
load y[i+2]
load y[i+3]
load x[i]
load x[i+1]
load x[i+2]
load x[i+3]
fma a * x[i] + y[i]
fma a * x[i+1] + y[i+1]
fma a * x[i+2] + y[i+2]
fma a * x[i+3] + y[i+2]
store y[i]
store y[i+1]
store y[i+2]
store y[i+3]
48
![Page 49: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/49.jpg)
Copyright (C) 2018 NVIDIA Corporation
Sequenced Before
constexpr float a = // ...std::vector<float> x = // ...std::vector<float> y = // ...
std::for_loop(std::execution::unseq,0, x.size(),[&] (int i) {y[i] += a * x[i];
});
vload y[i:i+3]
vload x[i:i+3]
vfma a * x[i:i+3] + y[i:i+3]
vstore y[i:i+3]
49
![Page 50: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/50.jpg)
Copyright (C) 2018 NVIDIA Corporation
Statements
Statements are compositions of full expressions.
50
![Page 51: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/51.jpg)
Copyright (C) 2018 NVIDIA Corporation
Statements
Statements are compositions of full expressions.
{statement0;statement1;// `statement0` is
// sequenced before
// `statement1`.
}
51
![Page 52: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/52.jpg)
Copyright (C) 2018 NVIDIA Corporation
Statements
Statements are compositions of full expressions.
{statement0;statement1;// `statement0` is
// sequenced before
// `statement1`.
}
if (condition)body;
// `condition` is
// sequenced before
// `body`.
52
![Page 53: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/53.jpg)
Copyright (C) 2018 NVIDIA Corporation
Statements
Statements are compositions of full expressions.
{statement0;statement1;// `statement0` is
// sequenced before
// `statement1`.
}
if (condition)body;
// `condition` is
// sequenced before
// `body`.
while (condition)body;
// Each evaluation of `condition`
// is sequenced before each
// evaluation of `body`.
53
![Page 54: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/54.jpg)
Copyright (C) 2018 NVIDIA Corporation
Function Evaluation
When calling a function…
1. Every evaluation within the function and every evaluation not
within the function are indeterminately sequenced.
[intro.execution] p11 s2
54
![Page 55: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/55.jpg)
Copyright (C) 2018 NVIDIA Corporation
Function Evaluation
When calling a function…
1. Every evaluation within the function and every evaluation not
within the function are indeterminately sequenced.
[intro.execution] p11 s2
2. The expression designating the function is sequenced before
the argument expressions.
[expr.call] p8 s1
55
![Page 56: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/56.jpg)
Copyright (C) 2018 NVIDIA Corporation
Function Evaluation
When calling a function…
1. Every evaluation within the function and every evaluation not
within the function are indeterminately sequenced.
[intro.execution] p11 s2
2. The expression designating the function is sequenced before
the argument expressions.
[expr.call] p8 s1
3. Each argument expression is indeterminately sequenced with
all other argument expressions.
[expr.call] p8 s2
56
![Page 57: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/57.jpg)
Copyright (C) 2018 NVIDIA Corporation
Function Evaluation
When calling a function…
1. Every evaluation within the function and every evaluation not
within the function are indeterminately sequenced.
[intro.execution] p11 s2
2. The expression designating the function is sequenced before
the argument expressions.
[expr.call] p8 s1
3. Each argument expression is indeterminately sequenced with
all other argument expressions.
[expr.call] p8 s2
4. Every expression in the body of the function is sequenced
after the expression designating the function and every
argument expression of the function .
[intro.execution] p11 s1
57
![Page 58: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/58.jpg)
Copyright (C) 2018 NVIDIA Corporation
Function Evaluation
void b(…) { e; }
g(a, (b)(c, d), f);
58
![Page 59: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/59.jpg)
Copyright (C) 2018 NVIDIA Corporation
Function Evaluation
a and e are indeterminately sequenced (Rule 1)
f and e are indeterminately sequenced (Rule 1)
void b(…) { e; }
g(a, (b)(c, d), f);
59
![Page 60: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/60.jpg)
Copyright (C) 2018 NVIDIA Corporation
Function Evaluation
a and e are indeterminately sequenced (Rule 1)
f and e are indeterminately sequenced (Rule 1)
(b) is sequenced before c and d (Rule 2)
void b(…) { e; }
g(a, (b)(c, d), f);
60
![Page 61: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/61.jpg)
Copyright (C) 2018 NVIDIA Corporation
Function Evaluation
a and e are indeterminately sequenced (Rule 1)
f and e are indeterminately sequenced (Rule 1)
(b) is sequenced before c and d (Rule 2)
c and d are indeterminately sequenced (Rule 3)
void b(…) { e; }
g(a, (b)(c, d), f);
61
![Page 62: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/62.jpg)
Copyright (C) 2018 NVIDIA Corporation
Function Evaluation
a and e are indeterminately sequenced (Rule 1)
f and e are indeterminately sequenced (Rule 1)
(b) is sequenced before c and d (Rule 2)
c and d are indeterminately sequenced (Rule 3)
c and d are sequenced before e (Rule 4)
void b(…) { e; }
g(a, (b)(c, d), f);
62
![Page 63: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/63.jpg)
Copyright (C) 2018 NVIDIA Corporation
Function Evaluation
a and e are indeterminately sequenced (Rule 1)
f and e are indeterminately sequenced (Rule 1)
(b) is sequenced before c and d (Rule 2)
c and d are indeterminately sequenced (Rule 3)
c and d are sequenced before e (Rule 4)
a – f are sequenced before the body of g (Rule 2)
void b(…) { e; }
g(a, (b)(c, d), f);
63
![Page 64: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/64.jpg)
Copyright (C) 2018 NVIDIA Corporation
Initializer Lists
Each element of a brace initializer is sequenced before
the all subsequent elements.
[dcl.init] p17
64
![Page 65: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/65.jpg)
Copyright (C) 2018 NVIDIA Corporation
Construction
struct A {A(int i) { cout << i; }
};
tuple t0 ( A(0), A(1) ) ;
tuple t1 { A(0), A(1) } ;
65
![Page 66: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/66.jpg)
Copyright (C) 2018 NVIDIA Corporation
Construction
struct A {A(int i) { cout << i; }
};
tuple t0 ( A(0), A(1) ) ;// GCC 8: “10”
tuple t1 { A(0), A(1) } ;// GCC 8: “01”
66
![Page 67: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/67.jpg)
Copyright (C) 2018 NVIDIA Corporation
Construction
struct A {A(int i) { cout << i; }
};
tuple t0 ( A(0), A(1) ) ;// GCC 8: “10”// LLVM 7: “01”
tuple t1 { A(0), A(1) } ;// GCC 8: “01”// LLVM 7: “01”
67
![Page 68: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/68.jpg)
Copyright (C) 2018 NVIDIA Corporation
Operator Evaluation
The value computations (but not the side effects) of an
operator are sequenced before the value computations
(but not the side effects) of its operands.
[intro.execution] p10 s2
68
![Page 69: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/69.jpg)
Copyright (C) 2018 NVIDIA Corporation
Operator Evaluation
The operand expressions to the following operators
are sequenced left to right:
E1 && E2 [expr.log.and] p2 s2
E1 || E2 [expr.log.or] p2 s2
E1 << E2 and E1 >> E2 [expr.shift] p4
E1, E2 [expr.comma] p1 s3
E1[E2] [expr.sub] p1 s6
E1.*E2 and E1->*E2 [expr.mptr.oper] p3 s3
69
![Page 70: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/70.jpg)
Copyright (C) 2018 NVIDIA Corporation
Operator Evaluation
The operands expressions to the following operators
operators are sequenced right to left:
E2 = E1 and E2 @= E1 [expr.ass] p1 s5
70
![Page 71: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/71.jpg)
Copyright (C) 2018 NVIDIA Corporation
Synchronizes With
Two library operations A and B may be related by the
synchronizes with relation.
[intro.races] p6
72
![Page 72: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/72.jpg)
Copyright (C) 2018 NVIDIA Corporation
Synchronizes With
Asymmetric: A synchronizes with B does not imply
that B synchronizes with A.
73
![Page 73: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/73.jpg)
Copyright (C) 2018 NVIDIA Corporation
Synchronizes With
Ways to achieve synchronizes with:
Atomic acquire/release.
Mutex lock/unlock.
Thread create/join.
Future/promise.
Parallel algorithm fork/join.
74
![Page 74: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/74.jpg)
Copyright (C) 2018 NVIDIA Corporation
Synchronizes With
T data = // ...atomic<bool> r(false);
data = ...r.store(1, memory_order_release);
if (r.load(memory_order_acquire)) {T tmp = data;// ...
}
Synchronizes with
75
![Page 75: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/75.jpg)
Copyright (C) 2018 NVIDIA Corporation
Synchronizes With
T data = // ...atomic<bool> r(false);
data = ...r.store(1, memory_order_release);
if (r.load(memory_order_acquire)) {T tmp = data;// ...
}
Synchronizes with
No
synchronizes with
data = ...r.store(1, memory_order_release);
if (r.load(memory_order_acquire)) {T tmp = data;// ...
}
76
![Page 76: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/76.jpg)
Copyright (C) 2018 NVIDIA Corporation
Synchronizes With
T data = // ...std::mutex mtx;
{ std::lock_guard l(mtx); // LockT tmp = data;// ...
} // Unlock
{ std::lock_guard l(mtx); // LockT tmp = data;// ...
} // Unlock
{ std::lock_guard l(mtx); // LockT tmp = data;// ...
} // Unlock
Synchronizes
with
77
![Page 77: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/77.jpg)
Copyright (C) 2018 NVIDIA Corporation
Happens Before
Given any two evaluations A and B…
If A happens before B:
78
![Page 78: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/78.jpg)
Copyright (C) 2018 NVIDIA Corporation
Happens Before
Given any two evaluations A and B…
If A happens before B:
A is sequenced before B, or
79
![Page 79: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/79.jpg)
Copyright (C) 2018 NVIDIA Corporation
Happens Before
Given any two evaluations A and B…
If A happens before B:
A is sequenced before B, or
A synchronizes with B, or
80
![Page 80: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/80.jpg)
Copyright (C) 2018 NVIDIA Corporation
Happens Before
Given any two evaluations A and B…
If A happens before B:
A is sequenced before B, or
A synchronizes with B, or
For some evaluation X, A happens before X and X
happens before B.
[intro.races] p7, p8, p9, p10
81
![Page 81: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/81.jpg)
Copyright (C) 2018 NVIDIA Corporation
Happens Before
Happens before doesn’t mean happened before.
82
![Page 82: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/82.jpg)
Copyright (C) 2018 NVIDIA Corporation
int x = 0;
int y = 0;
void foo(){x = y + 1;y = 1;
}
// GCC 8.2 –O3 x86-64
x:.zero 4
y:.zero 4
foo():movl y(%rip), %eaxmovl $1, y(%rip)addl $1, %eaxmovl %eax, x(%rip)ret
Happens Before
Source: Preshing on Programming 83
![Page 83: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/83.jpg)
Copyright (C) 2018 NVIDIA Corporation
Happens Before
Happens before doesn’t mean happened before.
Happening before doesn’t mean happens before.
84
![Page 84: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/84.jpg)
Copyright (C) 2018 NVIDIA Corporation
Happens Before
std::atomic<bool> ready = false;int data = 0;
std::thread producer([&] {data = 42;ready.store(true, memory_order_relaxed);
});
std::thread consumer([&] {if (ready.load(memory_order_relaxed))
std::cout << data;});
Source: Preshing on Programming 85
![Page 85: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/85.jpg)
Copyright (C) 2018 NVIDIA Corporation
Happens Before
Happens before describes arbitrary concatenations
of sequenced before and synchronizes with.
Atomic
Acquire
Release
Mutex
Lock
Unlock
…
Sequenced Before
(within a thread)
Synchronizes With
(between threads)
Happens Before
Consequence of
program order
86
![Page 86: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/86.jpg)
Copyright (C) 2018 NVIDIA Corporation
Execution Steps
The evaluations executed by threads are delineated by
execution steps.
[intro.progress] p3
87
![Page 87: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/87.jpg)
Copyright (C) 2018 NVIDIA Corporation
Execution Steps
The evaluations executed by threads are delineated by
execution steps.
[intro.progress] p3
An execution step is:
Termination of the thread.
88
![Page 88: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/88.jpg)
Copyright (C) 2018 NVIDIA Corporation
Execution Steps
The evaluations executed by threads are delineated by
execution steps.
[intro.progress] p3
An execution step is:
Termination of the thread.
An access of a volatile object.
89
![Page 89: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/89.jpg)
Copyright (C) 2018 NVIDIA Corporation
Execution Steps
The evaluations executed by threads are delineated by
execution steps.
[intro.progress] p3
An execution step is:
Termination of the thread.
An access of a volatile object.
Completion of:
A library I/O function call.
90
![Page 90: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/90.jpg)
Copyright (C) 2018 NVIDIA Corporation
Execution Steps
The evaluations executed by threads are delineated by
execution steps.
[intro.progress] p3
An execution step is:
Termination of the thread.
An access of a volatile object.
Completion of:
A library I/O function call.
A synchronization operation.
91
![Page 91: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/91.jpg)
Copyright (C) 2018 NVIDIA Corporation
Execution Steps
The evaluations executed by threads are delineated by
execution steps.
[intro.progress] p3
An execution step is:
Termination of the thread.
An access of a volatile object.
Completion of:
A library I/O function call.
A synchronization operation.
An atomic operation.
92
![Page 92: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/92.jpg)
Copyright (C) 2018 NVIDIA Corporation
Forward Progress
Some atomic operations may fail spuriously due to
interference from other threads.
Implementations are encouraged, but not required, to
prevent spurious failures from indefinitely delaying
progress.
[intro.progress] p2.2
93
![Page 93: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/93.jpg)
Copyright (C) 2018 NVIDIA Corporation
Forward Progress
Block: Wait for a condition to be satisfied before
continuing execution.
[defns.block]
94
![Page 94: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/94.jpg)
Copyright (C) 2018 NVIDIA Corporation
Forward Progress
Blocking library functions are considered to
continuously execute execution steps while waiting for
their condition to be satisfied.
Thus, blocking makes progress.
…Check
condition
false
trueDone
95
![Page 95: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/95.jpg)
Copyright (C) 2018 NVIDIA Corporation
Forward Progress
Forward progress guarantees that something
observable should eventually happens.
96
![Page 96: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/96.jpg)
Copyright (C) 2018 NVIDIA Corporation
Execution Steps
Implementations may assume that all threads will
eventually perform an execution step.
[intro.progress] p1
97
![Page 97: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/97.jpg)
Copyright (C) 2018 NVIDIA Corporation
Execution Steps
Implementations may assume that all threads will
eventually perform an execution step.
[intro.progress] p1
AKA infinite loops that have no observable effects are
undefined behavior.
98
![Page 98: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/98.jpg)
Copyright (C) 2018 NVIDIA Corporation
Forward Progress
Three classes of forward progress guarantees:
Concurrent forward progress.
Parallel forward progress.
Weakly parallel forward progress.
99
![Page 99: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/99.jpg)
Copyright (C) 2018 NVIDIA Corporation
Forward Progress
Concurrent forward progress: The thread will make
progress, regardless of whether other threads are
making progress.
[intro.progress] p7
100
![Page 100: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/100.jpg)
Copyright (C) 2018 NVIDIA Corporation
Forward Progress
Concurrent forward progress example:
Preemptive OS thread scheduling.
Unbounded thread pool.
Source: P0072 101
![Page 101: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/101.jpg)
Copyright (C) 2018 NVIDIA Corporation
Forward Progress
Parallel forward progress: Once the thread has
executed its first execution step, the thread will make
progress.
[intro.progress] p9
102
![Page 102: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/102.jpg)
Copyright (C) 2018 NVIDIA Corporation
Forward Progress
Parallel forward progress example:
Run-to-completion userspace tasking.
Bounded thread pool.
Threads on modern NVIDIA GPUs.
Source: P0072 103
![Page 103: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/103.jpg)
Copyright (C) 2018 NVIDIA Corporation
Forward Progress
Weakly parallel forward progress: The thread is not
guaranteed to make progress.
[intro.progress] p11
104
![Page 104: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/104.jpg)
Copyright (C) 2018 NVIDIA Corporation
Forward Progress
Weakly parallel forward progress example:
Non-preemptive OS thread scheduling.
Suspendable userspace tasking.
Work-stealing task schedulers.
Fibers.
Threadless asynchrony.
Lazy execution.
C++ coroutines.
Threads on legacy GPUs.
Source: P0072 105
![Page 105: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/105.jpg)
Copyright (C) 2018 NVIDIA Corporation
Forward Progress
Which guarantee does the main thread make?
[intro.progress] p8
106
![Page 106: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/106.jpg)
Copyright (C) 2018 NVIDIA Corporation
Forward Progress
Which guarantee does the main thread make?
Implementation defined.
[intro.progress] p8
107
![Page 107: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/107.jpg)
Copyright (C) 2018 NVIDIA Corporation
Forward Progress
Which guarantee do std::threads make?
[intro.progress] p8
108
![Page 108: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/108.jpg)
Copyright (C) 2018 NVIDIA Corporation
Forward Progress
Which guarantee do std::threads make?
Implementation defined.
[intro.progress] p8
109
![Page 109: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/109.jpg)
Copyright (C) 2018 NVIDIA Corporation
Boost Blocking
Boost Blocking: Block on a thread with weaker forward
progress while preserving the calling thread’s forward
progress.
[intro.progress] p14
110
![Page 110: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/110.jpg)
Copyright (C) 2018 NVIDIA Corporation
Boost Blocking
When a thread P boost blocks on a set S of other
threads, the forward progress guarantees of at least
one of the threads in S is temporarily upgraded to P’s
forward progress guarantee. Repeat until the blocking
condition is satisfied.
[intro.progress] p14
111
![Page 111: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/111.jpg)
Copyright (C) 2018 NVIDIA Corporation
Boost Blocking
Boost blocking ensures your children threads make
progress, not your siblings.
112
![Page 112: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/112.jpg)
Copyright (C) 2018 NVIDIA Corporation
Boost Blocking
struct lazy_thread {function<void()> f;
void join() {if (f) {
// Boost block by running the thread in the// calling thread.f();f = function<void()>{};
} else {throw make_error_code(errc::invalid_argument);
}}
};
113
![Page 113: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/113.jpg)
Copyright (C) 2018 NVIDIA Corporation
Boost Blocking
while (!empty && !done) {auto f = pop_next();f();
}
…
114
![Page 114: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/114.jpg)
Copyright (C) 2018 NVIDIA Corporation
Boost Blocking
while (!empty && !done) {auto f = pop_next();f();
}
…
115
![Page 115: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/115.jpg)
Copyright (C) 2018 NVIDIA Corporation
Boost Blocking
while (!empty && !done) {auto f = pop_next();f();
}
…
116
![Page 116: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/116.jpg)
Copyright (C) 2018 NVIDIA Corporation
Boost Blocking
static_thread_pool pool(…);
auto task = pool.execute_async(
);
117
![Page 117: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/117.jpg)
Copyright (C) 2018 NVIDIA Corporation
Boost Blocking
static_thread_pool pool(…);
auto task = pool.execute_async([&] {
});
118
![Page 118: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/118.jpg)
Copyright (C) 2018 NVIDIA Corporation
Boost Blocking
static_thread_pool pool(…);
auto task = pool.execute_async([&] {
auto child0 = pool.execute_async(…);auto child1 = pool.execute_async(…);…child0.join();child1.join();
});
119
![Page 119: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/119.jpg)
Copyright (C) 2018 NVIDIA Corporation
Boost Blocking
static_thread_pool pool(1);
auto task = pool.execute_async([&] {
auto child0 = pool.execute_async(…);auto child1 = pool.execute_async(…);…child0.join();child1.join();
});
120
![Page 120: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/120.jpg)
Copyright (C) 2018 NVIDIA Corporation
Summary
C++ Execution Model:
Threads evaluate expressions that access and
modify flat storage.
Evaluation within a thread is driven by sequenced
before relations.
Interactions between threads is driven by
synchronizes with relations.
Forward progress promises eventual termination.
121
![Page 121: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/121.jpg)
Copyright (C) 2018 NVIDIA Corporation
Caveats
C++17 and beyond
122
![Page 122: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/122.jpg)
Copyright (C) 2018 NVIDIA Corporation
Caveats
std::memory_order_consume
123
![Page 123: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/123.jpg)
Copyright (C) 2018 NVIDIA Corporation
Caveats
std::memory_order_consume
124
![Page 124: The C++ Execution Model - GitHub Pages · C++ programs describe operations that are performed on the abstract machine. C++ implementations define the characteristics of the abstract](https://reader031.vdocuments.us/reader031/viewer/2022040907/5e7e9cbe2f385a3d220697ef/html5/thumbnails/124.jpg)
Copyright (C) 2018 NVIDIA Corporation
Summary
C++ Execution Model:
Threads evaluate expressions that access and
modify flat storage.
Evaluation within a thread is driven by sequenced
before relations.
Interactions between threads is driven by
synchronizes with relations.
Forward progress promises eventual termination.
125
@blelbach