-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThread_condition_variable.h
More file actions
278 lines (212 loc) · 5.37 KB
/
Thread_condition_variable.h
File metadata and controls
278 lines (212 loc) · 5.37 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
#pragma once
/*
* 当一个线程需要依赖于另外一个线程时,可以采用多种选择,比如设置标志位,线程轮训检测标志位,如果为true,则往下执行,否则等待。
* 但是这种方法,未免过于效率低下,浪费cpu资源。 有人说可以使用sleep,不需要一直检测,这样可以提高cpu使用效率,但是带来一个问题,sleep多久才算是合理。
*
* c++11提供了一个办法,可以采用unique_lock和condition_variable的方法wait和notify_one\notify_all来进行同步。效率都要高于前两者。
* unique_lock的效率是低于lock_guard的,但是灵活性高。
*
* 同时如下示例,也提供了如何在类中对线程进行创建和处理,可用于面向对象编程。
*
* 性能对比:testThread_condition_variable() 对比使用notify与mutex检测之间的时间延迟,在windows上基本上没有什么差距。
*/
#include <thread>
#include <queue>
#include <iostream>
#include <mutex>
#include <condition_variable>
#include "./chronoStudy.h"
using namespace std;
string testResult;
struct timeConsume {
long send_time;
long receive_time;
long diff()
{
return receive_time - send_time;
}
};
long avg1 = 0;
long avg2 = 0;
long avg3 = 0;
class Thread_condition_variable
{
public:
void data_preparation_thread()
{
this_thread::sleep_for(chrono::milliseconds(500));
lock_guard<mutex> lk(mut);
data_queue.push(1);
tt.send_time = getMicroSeconds();
data_cond.notify_one();
}
void data_processing_thread()
{
unique_lock<mutex> lk(mut);
data_cond.wait(lk, [this] {return !data_queue.empty(); });
tt.receive_time = getMicroSeconds();
data_queue.pop();
lk.unlock();
}
void init()
{
tt.receive_time = 0;
tt.send_time = 0;
t0 = new thread(&Thread_condition_variable::data_preparation_thread,this);
std::this_thread::sleep_for(std::chrono::milliseconds(300));
t1 = new thread(&Thread_condition_variable::data_processing_thread, this);
t1->join();
t0->detach();
cout << "用notify_one来进行线程同步: 耗时微秒 = [" << tt.diff() << "]" << endl;
avg1 = (avg1 + tt.diff());
}
private:
mutex mut;
queue<int> data_queue;
condition_variable data_cond;
thread *t0;
thread *t1;
timeConsume tt;
};
class Thread_LockGuardmutex
{
public:
void data_preparation_thread()
{
lock_guard<mutex> lk(mut);
this_thread::sleep_for(chrono::milliseconds(500));
data_queue.push(1);
tt.send_time = getMicroSeconds();
}
void data_processing_thread()
{
lock_guard<mutex> lk(mut);
tt.receive_time = getMicroSeconds();
data_queue.pop();
}
void init()
{
tt.receive_time = 0;
tt.send_time = 0;
t0 = new thread(&Thread_LockGuardmutex::data_preparation_thread, this);
std::this_thread::sleep_for(std::chrono::milliseconds(300));
t1 = new thread(&Thread_LockGuardmutex::data_processing_thread, this);
t1->join();
t0->detach();
cout <<"用lock guard<mutex>进行线程同步: 耗时微秒 = ["<< tt.diff() <<"]" << endl;
avg2 = (avg2 + tt.diff());
}
private:
mutex mut;
queue<int> data_queue;
condition_variable data_cond;
thread *t0;
thread *t1;
timeConsume tt;
};
class Thread_mutex
{
public:
void data_preparation_thread()
{
mut.lock();
this_thread::sleep_for(chrono::milliseconds(500));
data_queue.push(1);
tt.send_time = getMicroSeconds();
mut.unlock();
}
void data_processing_thread()
{
mut.lock();
tt.receive_time = getMicroSeconds();
data_queue.pop();
mut.unlock();
}
void init()
{
tt.receive_time = 0;
tt.send_time = 0;
t0 = new thread(&Thread_mutex::data_preparation_thread, this);
std::this_thread::sleep_for(std::chrono::milliseconds(300));
t1 = new thread(&Thread_mutex::data_processing_thread, this);
t1->join();
t0->detach();
cout << "用mutex.lock进行线程同步: 耗时微秒 = [" << tt.diff() << "]" << endl;
avg3 = (avg3 + tt.diff());
}
private:
mutex mut;
queue<int> data_queue;
condition_variable data_cond;
thread *t0;
thread *t1;
timeConsume tt;
};
void testAvgResult(int i)
{
cout << "平均耗时:" << endl;
cout << avg1 / i << endl;
cout << avg2 / i << endl;
cout << avg3 / i << endl;
}
void testThread_condition_variable()
{
int i = 0; while (i < 60) {
cout << "------------------------------------------" << i << "---------------------" << endl;
Thread_condition_variable tcv;
tcv.init();
Thread_LockGuardmutex tlgm;
tlgm.init();
Thread_mutex tm;
tm.init(); i++; }
testAvgResult(i);
}
class Thread_condition
{
public:
void data_preparation_thread()
{
this_thread::sleep_for(chrono::milliseconds(500));
lock_guard<mutex> lk(mut);
data_queue.push(1);
tt.send_time = getMicroSeconds();
data_cond.notify_one();
cout << "after notify_one" << endl;
this_thread::sleep_for(chrono::milliseconds(10000));
cout << "after sleep" << endl;
}
void data_processing_thread()
{
unique_lock<mutex> lk(mut);
cout << "before wait" << endl;
data_cond.wait(lk, [this] {return !data_queue.empty(); });
cout << "after wait" << endl;
tt.receive_time = getMicroSeconds();
data_queue.pop();
lk.unlock();
}
void init()
{
tt.receive_time = 0;
tt.send_time = 0;
t0 = new thread(&Thread_condition::data_preparation_thread, this);
std::this_thread::sleep_for(std::chrono::milliseconds(300));
t1 = new thread(&Thread_condition::data_processing_thread, this);
t1->join();
t0->detach();
cout << "用notify_one来进行线程同步: 耗时微秒 = [" << tt.diff() << "]" << endl;
avg1 = (avg1 + tt.diff());
}
private:
mutex mut;
queue<int> data_queue;
condition_variable data_cond;
thread *t0;
thread *t1;
timeConsume tt;
};
void testThread_condition()
{
Thread_condition tcv;
tcv.init();
}