-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy path_test_boost_thread.cpp
More file actions
352 lines (270 loc) · 6.56 KB
/
_test_boost_thread.cpp
File metadata and controls
352 lines (270 loc) · 6.56 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
#include "stdafx.h"
#include <iostream>
#include <boost\thread\thread.hpp>
#include <boost\bind\bind.hpp>
#include <boost\thread\condition.hpp>
const int _buf_size = 10;
const int _iters = 100;
boost::mutex io_mutex;
/// @brief
class buffer
{
public:
buffer() :p(0), c(0), full(0)
{
}
~buffer()
{
}
void put(int m)
{
// boost::condition 은 unique_lock 을 사용해야 하기 때문에 scoped_lock 을 사용
// boost::mutex::sclped_lock 은 unique_lock 의 typedef 임
boost::mutex::scoped_lock lock(mutex);
if (full == _buf_size)
{
{
boost::mutex::scoped_lock lock2(io_mutex);
std::cout << "buffer is full. waiting..." << std::endl;
}
while (full == _buf_size)
{
cond.wait(lock); // unlock mutex and wait for cond.notify_xxx()
// cond automatically lock before return wait().
}
}
buf[p] = m;
p = (p+1) % _buf_size;
++full;
cond.notify_one(); // unlock and wake up waiting thread.
}
int get()
{
boost::mutex::scoped_lock lock(mutex);
if (full == 0)
{
{
boost::mutex::scoped_lock lock2(io_mutex);
std::cout << "buffer is empty. waiting..." << std::endl;
}
while (full == 0)
{
cond.wait(lock);
}
}
int i = buf[c];
c = (c+1) % _buf_size;
--full;
cond.notify_one();
return i;
}
private:
boost::mutex mutex;
boost::condition cond;
unsigned int p, c, full;
int buf[_buf_size];
};
/// @brief
void hello()
{
log_info "tid = %u, hello, boost thread.", GetCurrentThreadId() log_end
}
/// @brief
struct stcount
{
stcount(int id) : id(id)
{
}
void operator()()
{
for (int i = 0; i < 10; ++i)
{
boost::mutex::scoped_lock lock(io_mutex);
std::cout << id << ":" << i << std::endl;
}
}
int id;
};
/// @brief
void count(int id)
{
for (int i = 0; i < 10; ++i)
{
boost::mutex::scoped_lock lock(io_mutex);
std::cout << id << ":" << i << std::endl;
}
}
/// @brief
buffer buf;
void writer()
{
for (int n = 0; n < _iters; ++n)
{
{
boost::mutex::scoped_lock lock(io_mutex);
std::cout << "sending: " << n << std::endl;
}
buf.put(n);
}
}
void reader()
{
for (int x = 0; x < _iters; ++x)
{
int n = buf.get();
{
boost::mutex::scoped_lock lock(io_mutex);
std::cout << "received: " << n << std::endl;
}
}
}
class ConditionNotifyBeforeWait
{
public:
ConditionNotifyBeforeWait()
{
}
~ConditionNotifyBeforeWait()
{
}
void notifier()
{
boost::unique_lock<boost::mutex> lock(_lock);
_cond.notify_one();
log_info "notified" log_end;
}
void waiter()
{
boost::unique_lock<boost::mutex> lock(_lock);
boost::cv_status wait_ret = _cond.wait_for(lock, boost::chrono::seconds(1));
if (wait_ret == boost::cv_status::timeout)
{
log_info "wait time out" log_end;
}
else
{
log_info "awake" log_end;
}
}
/// cond.notify() 를 먼저 호출하고, cond.wait() 을 나중에 호출하면
/// 또는 그 반대의 경우 어떻게 되는지 테스트
void ConditionNotifyWaitTest()
{
/// wait 보다 notify 를 먼저 호출하면 깨어나지 않는다.
/// 당연한걸 뭐....
boost::thread t1(boost::bind(&ConditionNotifyBeforeWait::notifier,
this));
boost::thread t2(boost::bind(&ConditionNotifyBeforeWait::waiter,
this));
t1.join();
t2.join();
/// wait( ) -> notify() 하면 의도대로 잘 깨어나지
boost::thread t3(boost::bind(&ConditionNotifyBeforeWait::waiter,
this));
boost::thread t4(boost::bind(&ConditionNotifyBeforeWait::notifier,
this));
t3.join();
t4.join();
}
/// SetEvent(), WaitForSingleObject() 순서에 따라
/// 어떻게 동작하는지 테스트
///
/// 이벤트 객체는 당연히 상태가 유지되는 커널객체니까
/// Set 을 먼저하든 Wait 을 먼저하든 잘 되어야 정상
void EventSetAfterWait()
{
HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
_ASSERTE(NULL != event);
boost::thread t1([event]()
{
SetEvent(event);
});
boost::thread t2([event]()
{
Sleep(5000);
DWORD wait_ret = WaitForSingleObject(event, 1000);
if (WAIT_OBJECT_0 == wait_ret)
{
log_info "event signaled." log_end;
}
else
{
log_info "wait failed. ret=%u", wait_ret log_end;
}
});
t1.join();
t2.join();
CloseHandle(event);
}
void EventWaitAfterSet()
{
HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
_ASSERTE(NULL != event);
boost::thread t1([event]()
{
Sleep(1000);
DWORD wait_ret = WaitForSingleObject(event, 1000);
if (WAIT_OBJECT_0 == wait_ret)
{
log_info "event signaled." log_end;
}
else
{
log_info "wait failed. ret=%u", wait_ret log_end;
}
});
boost::thread t2([event]()
{
SetEvent(event);
});
t1.join();
t2.join();
CloseHandle(event);
}
boost::mutex _lock;
boost::condition_variable _cond;
};
void test_func(int timeout)
{
boost::this_thread::sleep_for(boost::chrono::milliseconds(timeout));
}
bool boost_try_join_for()
{
boost::thread t(test_func, 1000);
_ASSERTE(!t.try_join_for(boost::chrono::milliseconds(50)));
boost::thread tt(test_func, 3000);
_ASSERTE(t.try_join_for(boost::chrono::milliseconds(4000)));
return true;
}
/// @brief boost thread test
bool test_boost_thread()
{
//// #1 boost thread
//boost::thread t(&hello);
//log_info "tid = %u", t.get_id() log_end
//t.join();
//log_info "=======================" log_end
//// #2 boost::mutex
//boost::thread t1(stcount(1));
//boost::thread t2(stcount(2));
//t1.join();
//t2.join();
//log_info "=======================" log_end
//// #3 boost::mutex using bind
//boost::thread t12(boost::bind(&count, 1));
//boost::thread t22(boost::bind(&count, 2));
//t12.join();
//t22.join();
//log_info "=======================" log_end
//// #4 boost::condition
//boost::thread t13(&reader);
//boost::thread t23(&writer);
//t13.join();
//t23.join();
//ConditionNotifyBeforeWait cnbw;
//cnbw.ConditionNotifyWaitTest();
//cnbw.EventSetAfterWait();
//cnbw.EventWaitAfterSet();
boost_try_join_for();
return true;
}