-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAtomic.cpp
More file actions
50 lines (42 loc) · 1.12 KB
/
Copy pathAtomic.cpp
File metadata and controls
50 lines (42 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Demonstrate the usage of atomic variables and its memory model constraint which we can set.
*
* See https://gcc.gnu.org/wiki/Atomic/GCCMM/AtomicSync for more detail for each type of memory model
* we can set.
*/
#include <iostream>
#include <thread>
#include <atomic>
#include <cassert>
// variables that are shared among all threads created by this process
// note: constructor, and copy assignment operator is deleted
std::atomic<int> x;
std::atomic<int> y;
int local = 0;
static void t1Worker()
{
local = 1;
x.store(2);
std::cout << "done - thread 1" << std::endl;
}
static void t2Worker()
{
if (x.load() == 2)
{
std::cout << "modify" << std::endl;
assert(local == 1);
}
}
int main()
{
// start thread 1 first to make sure 'local' variable is set
// thus thread 2 will enter into if statement
// anyway you can defer 't1.join()' later after both threads started, but there is no guarantee
// which thread will start first
std::thread t1 (t1Worker);
t1.join();
std::thread t2 (t2Worker);
t2.join();
std::cout << "done" << std::endl;
return 0;
}