-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_atomic_counter.cpp
52 lines (44 loc) · 1.07 KB
/
test_atomic_counter.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
#include "simple_stopwatch.hpp"
#define BOOST_THREAD_VERSION 4
#define BOOST_THREAD_USES_LOG
#define BOOST_THREAD_USES_LOG_THREAD_ID
#include <boost/atomic.hpp>
#include <boost/thread/caller_context.hpp>
#include <boost/thread/detail/log.hpp>
#include <boost/thread/thread_only.hpp>
boost::atomic<int> g_cnt(5000000); // 5M => 220188364 nanoseconds
void writer()
{
Stopwatch sw;
for (;;) {
if (g_cnt > 0)
--g_cnt;
else
break;
}
BOOST_THREAD_LOG << BOOST_CURRENT_FUNCTION << sw.elapsed()
<< BOOST_THREAD_END_LOG;
}
void reader()
{
Stopwatch sw;
for (;;) {
if (g_cnt <= 0)
break;
}
BOOST_THREAD_LOG << BOOST_CURRENT_FUNCTION << sw.elapsed()
<< BOOST_THREAD_END_LOG;
}
int main()
{
Stopwatch sw;
boost::thread t0(writer);
boost::thread t1(reader);
boost::thread t2(writer);
t0.join();
t1.join();
t2.join();
BOOST_THREAD_LOG << BOOST_CURRENT_FUNCTION << sw.elapsed()
<< BOOST_THREAD_END_LOG;
return 0;
}