-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMemoryBarriers.cpp
More file actions
249 lines (186 loc) · 6.96 KB
/
MemoryBarriers.cpp
File metadata and controls
249 lines (186 loc) · 6.96 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// ===========================================================================
// MemoryBarriers.cpp
// ===========================================================================
#include "../Logger/Logger.h"
#include "../Logger/ScopedTimer.h"
#include <atomic>
#include <cassert>
#include <chrono>
#include <iostream>
#include <print>
#include <string>
#include <thread>
#include <vector>
namespace MemoryBarriersIntroduction {
static std::size_t g_data{};
static std::atomic<bool> g_ready{ false };
static void producerRelaxed()
{
std::thread::id tid{ std::this_thread::get_id() };
std::println("[{}] Writing ...", tid);
g_data = 123; // (1) write data
g_ready.store(true, std::memory_order_relaxed); // (2) publish flag
std::println("[{}] Writing done.", tid);
}
static void consumerRelaxed()
{
std::thread::id tid{ std::this_thread::get_id() };
std::println("[{}] Reading ...", tid);
if (g_ready.load(std::memory_order_relaxed)) { // (3) consume flag
std::println("[{}] Data: {}", tid, g_data); // (4) read data
}
std::println("[{}] Reading done.", tid);
}
static void test_memory_order_relaxed()
{
std::thread t1{ producerRelaxed };
std::thread t2{ consumerRelaxed };
t1.join();
t2.join();
}
// -----------------------------------------------------------------------------------
static void producerAcquireRelease()
{
std::thread::id tid{ std::this_thread::get_id() };
std::println("[{}] Writing ...", tid);
g_data = 123; // (1) write data
g_ready.store(true, std::memory_order_release); // (2) 'release'
std::println("[{}] Writing done.", tid);
}
static void consumerAcquireRelease()
{
std::thread::id tid{ std::this_thread::get_id() };
std::println("[{}] Reading ...", tid);
if (g_ready.load(std::memory_order_acquire)) { // (3) 'acquire'
std::println("[{}] Data: {}", tid, g_data); // (4) read data
}
std::println("[{}] Reading done.", tid);
}
static void test_memory_order_acquire_release()
{
std::thread t1{ producerAcquireRelease };
std::thread t2{ consumerAcquireRelease };
t1.join();
t2.join();
}
}
// -----------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------
namespace MemoryBarriers_RealWorldExample_Relaxed {
static std::atomic<int> requestCount{};
static constexpr std::size_t NumRequests{ 10'000 };
static void handle_request()
{
// get the job done...
// std::memory_order_relaxed: We just want the number to be incremented.
// We don't care in what order other threads see it
// or whether other variables are "synchronized".
requestCount.fetch_add(1, std::memory_order_relaxed);
}
static void test_memory_order_relaxed()
{
Logger::log(std::cout, "Begin");
ScopedTimer guard{};
std::vector<std::thread> threads;
threads.reserve(NumRequests);
for (std::size_t i{}; i != NumRequests; ++i) {
threads.emplace_back(handle_request);
}
for (auto& t : threads) {
t.join();
}
Logger::log(std::cout, "Total requests: ", requestCount.load(std::memory_order_relaxed));
Logger::log(std::cout, "Done.");
}
}
// -----------------------------------------------------------------------------------
namespace MemoryBarriers_RealWorldExample_ReleaseAcquire
{
static std::atomic<bool> g_ready{ false };
static std::string g_data{ "<empty>" }; // normal, non-atomare variable
static void producer()
{
std::thread::id tid{ std::this_thread::get_id() };
Logger::log(std::cout, "Producer: data = [", g_data, "]");
std::this_thread::sleep_for(std::chrono::seconds{ 3 });
Logger::log(std::cout, "Producer: writing data now ...");
// (1) Write at first data, non synchronized
g_data = "<secret password>";
// (2) Everything I did before this point,
// must be visible to anyone reading 'g_ready' with ACQUIRE
g_ready.store(true, std::memory_order_release);
Logger::log(std::cout, "Producer: Done.");
}
static void consumer()
{
std::thread::id tid{ std::this_thread::get_id() };
Logger::log(std::cout, "Consumer: data = [", g_data, "]");
// (3) ACQUIRE: I wait until 'ready' is true. Once that happens,
// I guarantee that I will also see all previous write accesses (such as 'data').
while (!g_ready.load(std::memory_order_acquire))
;
// (4) Secure access: data is guaranteed "Secret password"
Logger::log(std::cout, "Consumer: received data [", g_data, "]");
}
static void test_memory_order_acquire_release()
{
std::thread t1{ producer };
std::thread t2{ consumer };
t1.join();
t2.join();
}
}
// -----------------------------------------------------------------------------------
namespace MemoryBarriers_AnthonyWilliams
{
auto x{ std::atomic<bool>{} };
auto y{ std::atomic<bool>{} };
auto z{ std::atomic<std::size_t>{} };
auto order{ std::memory_order_seq_cst };
static void write_x()
{
x.store(true, order);
}
static void write_y()
{
y.store(true, order);
}
static void read_x_then_y()
{
while (!x.load(order));
if (y.load(order))
++z;
}
static void read_y_then_x()
{
while (!y.load(order));
if (x.load(order))
++z;
}
static void test_memory_order_anthony_williams_listing_5_4()
{
Logger::log(std::cout, "Start");
std::thread a{ write_x };
std::thread b{ write_y };
std::thread c{ read_x_then_y };
std::thread d{ read_y_then_x };
a.join();
b.join();
c.join();
d.join();
assert(z.load() != 0);
Logger::log(std::cout, "z: ", z.load());
Logger::log(std::cout, "Done.");
}
}
void test_memory_barriers()
{
MemoryBarriersIntroduction::test_memory_order_relaxed();
MemoryBarriersIntroduction::test_memory_order_acquire_release();
MemoryBarriers_RealWorldExample_Relaxed::test_memory_order_relaxed();
MemoryBarriers_RealWorldExample_ReleaseAcquire::test_memory_order_acquire_release();
MemoryBarriers_AnthonyWilliams::test_memory_order_anthony_williams_listing_5_4();
}
// ===========================================================================
// End-of-File
// ===========================================================================