-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
45 lines (37 loc) · 1000 Bytes
/
main.cpp
File metadata and controls
45 lines (37 loc) · 1000 Bytes
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
#include <iostream>
#include <assert.h>
#include <thread>
#include <atomic>
int main()
{
using namespace std::chrono_literals;
std::atomic_bool start (false);
// std::atomic_int val (0), flag (0);
int val = 0, flag = 0;
std::thread first ([&]() {
int old = val;
while(!start.load(std::memory_order_acquire)) continue;
val = 1;
val = 11;
flag = 1;
val = 12;
});
std::thread second ([&] () {
int w = val;
while(!start.load(std::memory_order_acquire)) continue;
int useless = val;
int _flag = flag;
int _val = val;
if (!( (_flag == 0 && _val == 0) || _val == 12)) {
std::cout << _flag << " : " << _val << " : " << useless << std::endl;
}
if (_flag) {
assert(_val > 10);
}
});
std::this_thread::sleep_for(10ms);
start.store(true, std::memory_order_release);
first.join();
second.join();
return 0;
}