Ongoing Optimisation with CppMem
I start with a small program and improve it successively. I verify each step of my process with CppMem. CppMem is an interactive tool for exploring the behaviour of small code snippets using the C++ memory model.
First, here is the small program.
1
// ongoingOptimisation.cpp
2
3
#include
<iostream>
4
#include
<thread>
5
6
int
x
=
0
;
7
int
y
=
0
;
8
9
void
writing
(){
10
x
=
2000
;
11
y
=
11
;
12
}
13
14
void
reading
(){
15
std
::
cout
<<
"y: "
<<
y
<<
" "
;
16
std
::
cout
<<
"x: "
<<
x
<<
std
::
endl
;
17
}
18
19
int
main
(){
20
std
::
thread
thread1
(
writing
);
21
std
::
thread
thread2
(
reading
);
22
thread1
.
join
();
23
thread2
.
join
();
24
}
The program is quite simple. It consists of the two threads thread1
and thread2
. thread1
writes the values x
and y
. thread2
reads the values x
and y
in the opposite sequence...