-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathmutex.cpp
268 lines (224 loc) · 4.93 KB
/
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
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
// Copyright (c) 2015
// Author: Chrono Law
#include <std.hpp>
//using namespace std;
using std::cout;
using std::endl;
#define BOOST_THREAD_VERSION 5
#include <boost/ref.hpp>
#include <boost/thread.hpp>
using namespace boost;
//////////////////////////////////////////
#include <boost/chrono.hpp>
using namespace boost::chrono;
seconds operator"" _s(unsigned long long n)
{
return seconds(n);
}
milliseconds operator"" _ms(unsigned long long n)
{
return milliseconds(n);
}
//////////////////////////////////////////
void case1()
{
mutex mu;
try
{
mu.lock();
cout << "some operations" << endl;
mu.unlock();
}
catch (...)
{
mu.unlock();
}
{
lock_guard<mutex> g(mu);
cout << "some operations" << endl;
}
}
//////////////////////////////////////////
void case2()
{
timed_mutex mu;
auto flag = mu.try_lock_for(100_ms);
if(flag)
{
cout << "lock timed mutex" << endl;
mu.unlock();
}
{
if(mu.try_lock_for(100_ms))
{
lock_guard<timed_mutex> g(mu, adopt_lock);
cout << "lock timed mutex" << endl;
}
}
}
//////////////////////////////////////////
#include <boost/thread/lock_factories.hpp>
template <typename Lockable, typename D>
unique_lock<Lockable> my_make_lock(Lockable& mtx, D d)
{
return unique_lock<Lockable> (mtx, d);
}
void case3()
{
mutex mu;
{
auto g = make_unique_lock(mu);
assert(g.owns_lock());
cout << "some operations" << endl;
}
{
auto g = make_unique_lock(mu, defer_lock);
assert(!g);
assert(g.try_lock());
assert(g);
cout << "some operations" << endl;
}
timed_mutex tm;
//typedef unique_lock<timed_mutex> lock_type;
{
//lock_type g(tm, 100_ms);
auto g = my_make_lock(tm, 100_ms);
if(g)
{
cout << "lock timed mutex" << endl;
}
}
auto g = make_unique_locks(mu, tm);
assert(std::tuple_size<decltype(g)>::value == 2);
}
//////////////////////////////////////////
#include <boost/atomic.hpp>
#include <boost/thread/lockable_adapter.hpp>
class account final : public lockable_adapter<mutex>
{
private:
boost::atomic<int> m_money{0};
public:
account() {}
~account() {}
public:
int sum() const
{
return m_money;
}
void withdraw(int x)
{
m_money -= x;
}
void deposit(int x)
{
m_money += x;
}
};
void case4()
{
account a;
{
auto g = make_unique_lock(a);
a.deposit(100);
a.withdraw(20);
assert(a.sum() == 80);
}
{
auto b = make_unique_lock(a, try_to_lock);
if(b)
{
a.withdraw(a.sum());
assert(a.sum() == 0);
}
}
}
//////////////////////////////////////////
void case5()
{
mutex m1, m2;
{
auto g1 = make_unique_lock(m1, adopt_lock);
auto g2 = make_unique_lock(m2, adopt_lock);
lock(m1, m2);
}
{
auto g1 = make_unique_lock(m1, defer_lock);
auto g2 = make_unique_lock(m2, defer_lock);
try_lock(g1, g2);
}
}
//////////////////////////////////////////
#include <boost/thread/lockable_concepts.hpp>
void case6()
{
BOOST_CONCEPT_ASSERT((BasicLockable<mutex>));
BOOST_CONCEPT_ASSERT((Lockable<mutex>));
BOOST_CONCEPT_ASSERT((Lockable<timed_mutex>));
BOOST_CONCEPT_ASSERT((TimedLockable<timed_mutex>));
BOOST_CONCEPT_ASSERT((Lockable<account>));
BOOST_CONCEPT_ASSERT((Lockable<lockable_adapter<mutex>>));
//BOOST_CONCEPT_ASSERT((Lockable<atomic<int>>));
}
//////////////////////////////////////////
#include <boost/thread/shared_lock_guard.hpp>
class rw_data
{
private:
int m_x;
shared_mutex rw_mu;
public:
rw_data():m_x(0){}
void write()
{
unique_lock<shared_mutex> g(rw_mu);
++m_x;
}
void read(int *x)
{
//shared_lock_guard<shared_mutex> g(rw_mu);
shared_lock<shared_mutex> g(rw_mu);
*x = m_x;
}
};
void writer(rw_data &d)
{
for (int i = 0;i < 20; ++i)
{
this_thread::sleep_for(3_ms);
d.write();
}
}
void reader(rw_data &d)
{
int x;
for (int i = 0;i < 10; ++i)
{
this_thread::sleep_for(5_ms);
d.read(&x);
cout << "reader:"<< x << endl;
}
}
void case7()
{
rw_data d;
thread_group pool;
pool.create_thread(bind(writer,boost::ref(d)));
pool.create_thread(bind(writer,boost::ref(d)));
pool.create_thread(bind(reader,boost::ref(d)));
pool.create_thread(bind(reader,boost::ref(d)));
pool.create_thread(bind(reader,boost::ref(d)));
pool.create_thread(bind(reader,boost::ref(d)));
pool.join_all();
}
//////////////////////////////////////////
int main()
{
case1();
case2();
case3();
case4();
case5();
case6();
case7();
}