-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
75 lines (62 loc) · 1.5 KB
/
test.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
#include <iostream>
#include <thread>
#include <future>
#include <chrono>
#include "blocking_queue.h"
#include <sys/time.h>
static double getMS(){
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec*1000.0 + tv.tv_usec/1000.0;
}
const size_t queueSize = 10;
template<typename T>
void thread1(BlockingQueue<T> &queue){
for(int i = 0; i < queueSize; ++i){
queue.Put(i);
std::this_thread::sleep_for(std::chrono::milliseconds(18));
}
}
template<typename T>
void thread2(BlockingQueue<T> &queue, BlockingQueue<T>& queue2){
int k = 0;
while(1){
while(queue.Size()){
T front = queue.Take();
queue2.Put(front);
std::this_thread::sleep_for(std::chrono::milliseconds(37));
k++;
}
if(k==queueSize) break;}
}
template<typename T>
void thread3(BlockingQueue<T>& queue){
int k = 0;
while(1){
while(queue.Size()){
queue.Take();
std::this_thread::sleep_for(std::chrono::milliseconds(18));
k++;
}
if(k==queueSize)break;}
}
int main(int argc, char* argv[]){
BlockingQueue<int> queue;
BlockingQueue<int> queue2;
double st = getMS();
std::thread t1(thread1<int>,std::ref(queue));
std::thread t2(thread2<int>,std::ref(queue),std::ref(queue2));
std::thread t3(thread3<int>,std::ref(queue2));
t1.join();
t2.join();
t3.join();
double et = getMS();
std::cout << "Muil threads use time: " << et-st << "ms" << std::endl;
st = getMS();
thread1(queue);
thread2(queue,queue2);
thread3(queue2);
et = getMS();
std::cout << "Single thread use time: " << et-st << "ms" << std::endl;
return 0;
}