-
Notifications
You must be signed in to change notification settings - Fork 0
/
notif_acq_rel.cpp
52 lines (48 loc) · 940 Bytes
/
notif_acq_rel.cpp
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
51
52
#ifdef USE_VALGRIND
#include <valgrind/helgrind.h>
#include <valgrind/drd.h>
#endif
#include <atomic>
#include <iostream>
#include <optional>
#include <string>
#include <thread>
/**
* no problem in this code
*/
int main(int argc, char* argv[])
{
size_t nb = 1;
if (argc >= 2)
{
nb = std::stoul(argv[1]);
}
for (size_t i = 0; i < nb; i++)
{
std::atomic<bool> done{false};
std::optional<int> payload;
std::thread t1([&] {
while (!done.load(std::memory_order_acquire)) // acquire
{
}
#ifdef USE_VALGRIND
ANNOTATE_HAPPENS_AFTER(&done);
#endif
// use the payload
if (payload)
{
std::cout << *payload << "\n";
}
});
std::thread t2([&] {
payload = 19;
#ifdef USE_VALGRIND
ANNOTATE_HAPPENS_BEFORE(&done);
#endif
done.store(true, std::memory_order_release); // release
});
t1.join();
t2.join();
}
return 0;
}