-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_std_future_async.cpp
More file actions
352 lines (293 loc) · 5.67 KB
/
test_std_future_async.cpp
File metadata and controls
352 lines (293 loc) · 5.67 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
* @file _test_std_future_async.cpp
* @brief
*
* @author Yonhgwhan, Roh (somma@somma.kr)
* @date 02/18/2023 Created.
* @copyright (C)Somma, Inc. All rights reserved.
**/
#include "stdafx.h"
#include <future>
#include <thread>
#include <chrono>
#include <queue>
///
///
///
void test_condition_variable()
{
_mem_check_begin
{
std::mutex m;
std::condition_variable cv;
bool done = false;
std::string info;
std::thread t([&]() {
{
std::lock_guard<std::mutex> lk(m);
info = "some data";
done = true;
}
cv.notify_all();
});
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, [&] {return done; });
lk.unlock();
log_info "data=%s", info.c_str() log_end;
t.join();
}
_mem_check_end;
}
///
///
///
void
producer(
_In_ std::queue<std::string>* pages,
_In_ std::mutex* lock,
_In_ int index,
_In_ std::condition_variable* cv
)
{
for (int i = 0; i < 5; ++i)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100 * index));
std::string content =
"index " + std::to_string(i) +
" from thread " + std::to_string(index);
lock->lock();
pages->push(content);
lock->unlock();
// consumer 에게 content 가 추가되었음을 알린다.
cv->notify_one();
}
}
void consumer(
_In_ std::queue<std::string>* pages,
_In_ std::mutex* lock,
_In_ int* num_processed,
_In_ std::condition_variable* cv
)
{
while (*num_processed < 25)
{
std::unique_lock<std::mutex> lk(*lock);
cv->wait(
lk,
[&](){
// 조건이 거짓인 경우 unlock 하고, sleep 한다.
// -> true 를 리턴하면 깨어나고,
// -> false 를 리턴하면 계속 sleep
return !pages->empty() || *num_processed == 25;
}
);
//
// lock 된 상태로 깨어났음
//
if (*num_processed == 25)
{
lk.unlock();
return;
}
std::string content = pages->front();
pages->pop();
++(*num_processed);
lk.unlock(); //<!
log_info
"content: %s",
content.c_str()
log_end;
}
}
void test_condition_variable_producer_consumer()
{
_mem_check_begin
{
std::queue<std::string> pages;
std::mutex pages_lock;
std::condition_variable cv;
std::vector<std::thread> producers;
for (int i = 0; i < 5; ++i)
{
producers.push_back(
std::thread(
producer,
&pages,
&pages_lock,
i + 1,
&cv)
);
}
int num_processed = 0;
std::vector<std::thread> consumers;
for (int i = 0; i < 3; ++i)
{
consumers.push_back(
std::thread(
consumer,
&pages,
&pages_lock,
&num_processed,
&cv)
);
}
for (int i = 0; i < 5; ++i)
{
producers[i].join();
}
// consumer 들을 모두 깨운다.
cv.notify_all();
for (int i = 0; i < 3; ++i)
{
consumers[i].join();
}
}
_mem_check_end;
}
/// @brief cv->notify_one() 호출갯수와 cv->wait() 이 깨어나는 수는 보장이 됨
/// notify_one 과 wait 이 깨어나는 횟수가 걱정되면 wait_for 쓰면 됨
void test_condition_variable_producer_consumer_2()
{
_mem_check_begin
{
std::queue<std::string> pages;
std::mutex pages_lock;
std::condition_variable cv;
int num_produced = 0;
int num_consumed = 0;
// producer
auto p = std::thread([&]() {
for (int i = 0; i < 1000; ++i)
{
pages_lock.lock();
pages.push("content " + std::to_string(i));
++num_produced;
pages_lock.unlock();
cv.notify_one();
}
});
// consumer
auto c = std::thread([&]() {
while (num_consumed != num_produced)
{
std::unique_lock<std::mutex> lock(pages_lock);
cv.wait(lock, [&]() {
return !pages.empty(); // content 가 있으면 깨어남
});
std::string content = pages.front();
pages.pop();
++num_consumed;
lock.unlock(); //<!
log_info
"content: %s",
content.c_str()
log_end;
}
});
p.join();
// consumer 들을 모두 깨운다.
cv.notify_all();
c.join();
_ASSERTE(num_produced == num_consumed);
}
_mem_check_end;
}
///
///
///
void worker(std::promise<std::string>* p)
{
p->set_value("somma_data");
}
void test_future()
{
_mem_check_begin
{
std::promise<std::string> p;
std::future<std::string> data = p.get_future();
std::thread t(worker, &p);
data.wait();
log_info "data=%s", data.get().c_str() log_end;
t.join();
}
_mem_check_end;
}
///
///
///
void test_future_exception()
{
_mem_check_begin
{
std::promise<std::string> p;
std::future<std::string> data = p.get_future();
std::thread t([](std::promise<std::string>* p) {
try
{
throw std::runtime_error("Some error");
}
catch (...)
{
p->set_exception(std::current_exception());
}
},
&p);
data.wait();
try
{
log_info "data=%s", data.get().c_str() log_end;
}
catch (const std::exception& e)
{
log_err "exception: %s", e.what() log_end;
}
t.join();
}
_mem_check_end;
}
///
///
///
void test_future_wait_for()
{
_mem_check_begin
{
std::promise<void> p;
std::future<void> data = p.get_future();
std::thread t([](std::promise<void>* p) {
std::this_thread::sleep_for(std::chrono::seconds(10));
p->set_value();
},
&p
);
while (true)
{
std::future_status status = data.wait_for(std::chrono::seconds(1));
// 아직 준비 안되었음
if (status == std::future_status::timeout)
{
log_dbg ">" log_end;
}
// 준비 완료
else if (status == std::future_status::ready)
{
log_dbg "" log_end;
break;
}
}
t.join();
}
_mem_check_end;
}
/// @brief test suit
bool test_std_future_async()
{
//test_condition_variable();
//test_condition_variable_producer_consumer();
test_condition_variable_producer_consumer_2();
//test_future();
//test_future_exception();
//
//test_future_wait_for();
return true;
}