-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_shared_mutex.cpp
70 lines (62 loc) · 1.7 KB
/
test_shared_mutex.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "simple_stopwatch.hpp"
#define BOOST_THREAD_VERSION 4
#define BOOST_THREAD_USES_LOG
#define BOOST_THREAD_USES_LOG_THREAD_ID
#include <boost/thread.hpp>
#include <boost/thread/caller_context.hpp>
#include <boost/thread/detail/log.hpp>
boost::shared_mutex
mtx; // warning: initialization of 'mutex' with static storage duration
// may throw an exception that cannot be caught [cert-err58-cpp]
int g_cnt(5000000 / 10); // 5M/10 => 4005472571 nanoseconds
void writer()
{
Stopwatch sw;
for (;;) {
boost::upgrade_lock<boost::shared_mutex> readlock(mtx);
boost::upgrade_to_unique_lock<boost::shared_mutex> writelock(readlock);
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 (;;) {
boost::shared_lock<boost::shared_mutex> readlock(mtx);
if (g_cnt <= 0)
break;
}
BOOST_THREAD_LOG << BOOST_CURRENT_FUNCTION << sw.elapsed()
<< BOOST_THREAD_END_LOG;
}
void unique_locker()
{
Stopwatch sw;
for (;;) {
boost::unique_lock<boost::shared_mutex> lock(mtx);
if (g_cnt > 0)
--g_cnt;
else
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(unique_locker);
t0.join();
t1.join();
t2.join();
BOOST_THREAD_LOG << BOOST_CURRENT_FUNCTION << sw.elapsed()
<< BOOST_THREAD_END_LOG;
return 0;
}