-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_shared_mutex.cc
124 lines (98 loc) · 2.88 KB
/
test_shared_mutex.cc
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
#include <iostream>
#include <iomanip>
#include <mutex>
#include <string>
#include <thread>
#include <chrono>
#include <random>
#include <atomic>
#include <boost/thread/shared_mutex.hpp>
//#include <boost/thread/unique_lock.hpp>
//#include <boost/thread/lock_types.hpp>
static bool done = false;
struct Nanocube {
void write(int value) {
++write_count;
data.push_back(value);
}
int read(int index) {
++read_count;
return data[index % data.size()];
}
std::vector<int> data;
mutable boost::shared_mutex mutex;
std::atomic<uint64_t> read_count;
std::atomic<uint64_t> write_count;
};
struct NanocubeAccess {
NanocubeAccess(Nanocube &nanocube, std::string name):
nanocube ( nanocube ),
name ( name )
{}
Nanocube &nanocube;
std::string name;
};
struct Writer: public NanocubeAccess {
using NanocubeAccess::NanocubeAccess;
void run() {
int count = 1;
while (!done) {
{
boost::unique_lock<boost::shared_mutex> lock(nanocube.mutex);
for (int i=0;i<100;i++) {
nanocube.write(count++);
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
};
struct Reader: public NanocubeAccess {
using NanocubeAccess::NanocubeAccess;
void run() {
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(1,100);
while (!done) {
{
boost::shared_lock<boost::shared_mutex> lock(nanocube.mutex);
nanocube.read(nanocube.data.size()-1);
}
std::this_thread::sleep_for(std::chrono::milliseconds(distribution(generator)));
}
}
};
struct Logger: public NanocubeAccess {
using NanocubeAccess::NanocubeAccess;
void run() {
while (!done) {
std::cout << "reads: " << std::setw(10) << nanocube.read_count
<< ", writes: " << std::setw(10) << nanocube.write_count
<< std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2));
}
}
};
int main() {
Nanocube nanocube;
nanocube.write(0);
std::vector<std::thread> threads;
threads.push_back(std::thread(&Writer::run, Writer(nanocube, "Writer")));
// start writer thread
for (int i=0;i<10;i++) {
threads.push_back(std::thread(&Reader::run, Reader(nanocube, "Reader_" + std::to_string(i))));
}
threads.push_back(std::thread(&Logger::run, Logger(nanocube, "Logger")));
char ch_done = 'x';
while (!done) {
std::cin >> ch_done;
if (ch_done == 'q') {
done = true;
}
}
std::cout << "Joining threads" << std::endl;
for (auto &th: threads) {
th.join();
std::cout << "thread done" << std::endl;
}
return 0;
}