-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathmain.cpp
More file actions
151 lines (132 loc) · 3.26 KB
/
main.cpp
File metadata and controls
151 lines (132 loc) · 3.26 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
//
// Created by Alone on 2022-8-19.
//
#include "../benchmark/Timer.hpp"
#include<vector>
#include<iostream>
#include<thread>
#define V1
#ifdef UTIL
#include "theadsafe_queue.h"
using namespace util;
#endif
#ifdef V1
#include "v1/ThreadSafeQueue.h"
using namespace v1;
#endif
#ifdef V3
#include "v3/ThreadSafeQueue.h"
using namespace v3;
#endif
void test_multi_producer()
{
Timer tm;
ThreadSafeQueue<std::vector<int>> mq;
ThreadSafeQueue<int> print_mq;
std::thread producer([&]
{
for (int i = 0; i < 1500000; i++)
{
mq.Push(std::vector<int>{3,23,323,432,4324,32432,4324,3532,532532,5325,32532,5325,325234,23432,4324,23432,324,32432,4324,32432,4324,324324,3423,4324});
}
});
std::thread t1([&]()
{
for (int i = 0; i < 500000; i++)
{
auto ptr = mq.WaitAndPop();
print_mq.Push(ptr->front());
}
});
std::thread t2(
[&]()
{
for (int i = 0; i < 500000; i++)
{
auto ptr = mq.WaitAndPop();
print_mq.Push(ptr->front());
}
}
);
std::thread t3(
[&]()
{
int x;
for (int i = 0; i < 50000; i++)
{
auto ptr = mq.WaitAndPop();
print_mq.Push(ptr->front());
}
}
);
std::thread t4(
[&]
{
int x;
for (int i = 0; i < 200000; i++)
{
print_mq.WaitAndPop(x);
std::cout << x << "\n";
}
}
);
producer.join();
t1.join();
t2.join();
t3.join();
t4.join();
}
void test_one_producer()
{
Timer tm;
ThreadSafeQueue<int> mq;
ThreadSafeQueue<int> print_mq;
std::thread producer(
[&]
{
for (int i = 0; i < 15000; i++)
{
mq.Push(i);
}
});
std::thread t1([&]()
{
int x;
for (int i = 0; i < 5000; i++)
{
mq.WaitAndPop(x);
std::cout << x << "\n";
}
});
std::thread t2(
[&]()
{
int x;
for (int i = 0; i < 5000; i++)
{
mq.WaitAndPop(x);
std::cout << x << "\n";
}
}
);
std::thread t3(
[&]()
{
int x;
for (int i = 0; i < 5000; i++)
{
mq.WaitAndPop(x);
std::cout << x << "\n";
}
}
);
producer.join();
t1.join();
t2.join();
t3.join();
}
int main()
{
freopen("../../test_source/thread_output_text", "w", stdout);
test_multi_producer();
}