-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_test2.cpp
More file actions
238 lines (209 loc) · 3.92 KB
/
thread_test2.cpp
File metadata and controls
238 lines (209 loc) · 3.92 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
//
#include "worker2.h"
#include <atomic>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
namespace
{
//
std::atomic_int keeper{100};
void
test1()
{
class testWorker : public SimpleWorker::Worker
{
std::atomic_int* num;
public:
void set(std::atomic_int* np) { num = np; }
void run() override
{
auto prev = num->fetch_add(1);
if (prev % 10 == 0)
{
std::cout << "run: " << prev << std::endl;
keeper.fetch_sub(1);
}
}
};
std::vector<testWorker> twList;
std::atomic_int count{0};
class keeperWorker : public SimpleWorker::Worker
{
public:
void run() override { std::cout << "keeper run" << std::endl; }
};
keeperWorker kpw;
{
SimpleWorker::WorkerThread wt{};
wt.push(&keeper, kpw);
twList.resize(1000);
for (int i = 0; i < twList.size(); i++)
{
auto& w = twList[i];
w.set(&count);
wt.push(w);
}
wt.execute();
wt.wait();
}
std::cout << count.load() << std::endl;
}
//
class ChildWorker : public SimpleWorker::Worker
{
public:
void run() override { std::cout << "child" << std::endl; }
};
class CheckWorker : public SimpleWorker::Worker
{
public:
void run() override { std::cout << "check" << std::endl; }
};
std::atomic_int mainCount{};
std::atomic_int childCount{};
std::vector<ChildWorker> children;
CheckWorker wkCheck;
void
test2()
{
class MainWorker : public SimpleWorker::Worker
{
SimpleWorker::WorkerThread& wt;
public:
MainWorker(SimpleWorker::WorkerThread& w) : wt(w) {}
void run() override
{
std::cout << "start" << std::endl;
children.resize(100);
childCount = children.size();
mainCount.fetch_add(1);
wt.push(&childCount, wkCheck, &mainCount);
for (auto& ch : children)
{
wt.push(ch, &childCount);
}
}
};
class PostWorker : public SimpleWorker::Worker
{
public:
void run() override { std::cout << "post" << std::endl; }
};
SimpleWorker::WorkerThread wt{};
MainWorker wkMain{wt};
PostWorker wkPost;
mainCount = 1;
wt.push(&mainCount, wkPost);
wt.push(wkMain, &mainCount);
wt.execute();
wt.wait();
}
//
std::atomic_int exec{0};
std::atomic_int start{0};
std::atomic_int end{0};
std::vector<int> buffer;
//
bool
push(int p)
{
int n, e;
do
{
e = end.load();
n = (e + 1) % buffer.size();
if (n == start)
{
exec = 0;
return false;
}
} while (end.compare_exchange_weak(e, n) == false);
buffer[e] = p;
exec = 0;
return true;
}
int
pop()
{
int s = start;
if (s == end)
{
return -1;
}
int n = (s + 1) % buffer.size();
if (start.compare_exchange_weak(s, n) == false)
{
return -1;
}
auto p = buffer[s];
buffer[s] = -1;
return p;
}
void
test3()
{
buffer.resize(500);
std::fill(buffer.begin(), buffer.end(), -1);
std::condition_variable cond;
std::mutex mtx;
std::atomic_bool en;
auto run = [&]()
{
{
std::unique_lock lk(mtx);
cond.wait(lk);
}
for (int i = 0; i < buffer.size() / 3; i++)
{
push(i);
std::cout << "push" << std::endl;
}
};
auto get = [&]()
{
{
std::unique_lock lk(mtx);
cond.wait(lk);
}
while (en)
{
for (;;)
{
int n = pop();
if (n == -1)
{
break;
}
else
{
std::cout << n << std::endl;
}
}
}
};
en = true;
auto t4 = std::thread([&] { get(); });
auto t1 = std::thread([&] { run(); });
auto t2 = std::thread([&] { run(); });
auto t3 = std::thread([&] { run(); });
std::this_thread::sleep_for(std::chrono::microseconds(500));
cond.notify_all();
t1.join();
t2.join();
t3.join();
en = false;
t4.join();
}
} // namespace
//
int
main(int argc, char** argv)
{
test1();
test2();
test3();
return 0;
}