-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththreadpool.hpp
More file actions
384 lines (324 loc) · 10.8 KB
/
threadpool.hpp
File metadata and controls
384 lines (324 loc) · 10.8 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#pragma once
#include "thread.hpp"
#include <future>
#include <queue>
class ThreadPool : noncopyable
{
public:
using Task = Thread::Task; // 统一使用 Thread::Task 类型
explicit ThreadPool(size_t threadCount = std::thread::hardware_concurrency(),
size_t maxQueueSize = 1000)
: m_maxQueueSize(maxQueueSize == 0 ? 1 : maxQueueSize)
{
if (threadCount == 0) {
threadCount = 1;
}
initializeWorkers(threadCount);
}
~ThreadPool() { shutdownNow(); }
// 提交任务(无返回值)
template<typename F>
auto submit(F &&task) -> bool
{
return submitInternal(std::forward<F>(task), false, std::chrono::milliseconds(0));
}
// 尝试提交任务(非阻塞)
template<typename F>
auto trySubmit(F &&task) -> bool
{
return submitInternal(std::forward<F>(task), true, std::chrono::milliseconds(0));
}
// 带超时的任务提交
template<typename F, typename Rep, typename Period>
auto submitFor(F &&task, const std::chrono::duration<Rep, Period> &timeout) -> bool
{
return submitInternal(std::forward<F>(task),
false,
std::chrono::duration_cast<std::chrono::milliseconds>(timeout));
}
// 提交接受 stop_token 的任务并返回 future
template<typename F, typename... Args>
auto submitFuture(F &&f, Args &&...args)
-> std::future<std::invoke_result_t<F, std::stop_token, Args...>>
{
using return_type = std::invoke_result_t<F, std::stop_token, Args...>;
auto task = std::make_shared<std::packaged_task<return_type(std::stop_token)>>(
[func = std::forward<F>(f),
args = std::make_tuple(std::forward<Args>(args)...)](std::stop_token token) mutable {
return std::apply(func, std::tuple_cat(std::make_tuple(token), std::move(args)));
});
std::future<return_type> result = task->get_future();
bool success = submit([task](std::stop_token token) { (*task)(token); });
if (!success) {
std::promise<return_type> p;
p.set_exception(std::make_exception_ptr(
std::runtime_error("ThreadPool is stopped or queue is full")));
return p.get_future();
}
return result;
}
// 等待所有任务完成
void waitAll()
{
std::unique_lock<std::mutex> lock(m_mutex);
m_condAllDone.wait(lock, [this]() {
return m_totalTasks == 0 && m_taskQueue.empty() && m_runningTasks == 0;
});
}
// 带超时的等待
bool waitAllFor(std::chrono::milliseconds timeout = std::chrono::milliseconds::max())
{
std::unique_lock<std::mutex> lock(m_mutex);
return m_condAllDone.wait_for(lock, timeout, [this]() {
return m_totalTasks == 0 && m_taskQueue.empty() && m_runningTasks == 0;
});
}
// 优雅关闭 - 等待所有任务完成
void shutdown()
{
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_stop) {
return; // 已经在关闭过程中
}
m_stop = true;
}
// 通知所有条件变量(不需要在锁内通知)
m_condEmpty.notify_all();
m_condFull.notify_all();
// 停止所有工作线程
for (auto &worker : m_workers) {
if (worker->isJoinable()) {
worker->requestStop();
}
}
// 等待所有工作线程结束
for (auto &worker : m_workers) {
if (worker->isJoinable()) {
worker->waitForFinished(std::chrono::seconds(5));
}
}
// 清空任务队列
{
std::lock_guard<std::mutex> lock(m_mutex);
std::queue<Task>().swap(m_taskQueue);
m_totalTasks = 0;
m_runningTasks = 0;
}
m_condAllDone.notify_all();
}
// 立即关闭 - 丢弃未执行的任务
void shutdownNow()
{
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_stop) {
return;
}
m_stop = true;
// 清空任务队列
std::queue<Task>().swap(m_taskQueue);
m_totalTasks = 0;
}
// 通知所有条件变量
m_condEmpty.notify_all();
m_condFull.notify_all();
m_condAllDone.notify_all();
// 停止所有工作线程
for (auto &worker : m_workers) {
if (worker->isJoinable()) {
worker->requestStop();
}
}
// 等待工作线程结束(带超时)
for (auto &worker : m_workers) {
if (worker->isJoinable()) {
if (!worker->waitForFinished(std::chrono::seconds(3))) {
// 超时,强制停止
worker->stop();
}
}
}
m_workers.clear();
}
// 重启线程池
bool restart(size_t threadCount = std::thread::hardware_concurrency())
{
// 先关闭
shutdownNow();
// 重置状态
{
std::lock_guard<std::mutex> lock(m_mutex);
m_stop = false;
m_runningTasks = 0;
m_totalTasks = 0;
}
// 创建新的工作线程
return initializeWorkers(threadCount);
}
// 获取线程池状态
[[nodiscard]] auto isRunning() const -> bool
{
std::lock_guard<std::mutex> lock(m_mutex);
return !m_stop;
}
[[nodiscard]] auto isStopped() const -> bool
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_stop;
}
[[nodiscard]] auto size() const -> size_t { return m_workers.size(); }
[[nodiscard]] auto queueSize() const -> size_t
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_taskQueue.size();
}
[[nodiscard]] auto getMaxQueueSize() const -> size_t
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_maxQueueSize;
}
[[nodiscard]] auto getRunningTasks() const -> size_t
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_runningTasks;
}
[[nodiscard]] auto getPendingTasks() const -> size_t
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_taskQueue.size();
}
[[nodiscard]] auto getTotalTasks() const -> size_t
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_totalTasks;
}
// 设置最大队列大小
void setMaxQueueSize(size_t maxSize)
{
{
std::lock_guard<std::mutex> lock(m_mutex);
m_maxQueueSize = maxSize == 0 ? 1 : maxSize;
}
m_condFull.notify_all();
}
private:
bool initializeWorkers(size_t threadCount)
{
try {
m_workers.clear();
for (size_t i = 0; i < threadCount; ++i) {
auto worker = std::make_unique<Thread>(
[this](std::stop_token token) { workerThread(token); });
if (!worker->start()) {
shutdownNow();
return false;
}
m_workers.push_back(std::move(worker));
}
return true;
} catch (...) {
shutdownNow();
return false;
}
}
template<typename F>
bool submitInternal(F &&task, bool nonBlocking, std::chrono::milliseconds timeout)
{
// 快速检查是否已停止
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_stop) {
return false;
}
}
{
std::unique_lock<std::mutex> lock(m_mutex);
// 检查队列是否已满
if (m_maxQueueSize > 0 && m_taskQueue.size() >= m_maxQueueSize) {
if (nonBlocking) {
return false;
}
if (timeout.count() > 0) {
// 带超时等待
if (!m_condFull.wait_for(lock, timeout, [this]() {
return m_stop || m_taskQueue.size() < m_maxQueueSize;
})) {
return false; // 超时
}
} else {
// 无限等待
m_condFull.wait(lock, [this]() {
return m_stop || m_taskQueue.size() < m_maxQueueSize;
});
}
}
if (m_stop) {
return false;
}
m_taskQueue.push(std::forward<F>(task));
m_totalTasks++;
}
m_condEmpty.notify_one();
return true;
}
void workerThread(std::stop_token token)
{
while (true) {
Task task;
{
std::unique_lock<std::mutex> lock(m_mutex);
// 等待任务或停止信号
m_condEmpty.wait(lock, [this, &token]() {
return !m_taskQueue.empty() || m_stop || token.stop_requested();
});
// 检查停止条件
if (m_stop || token.stop_requested()) {
break;
}
if (m_taskQueue.empty()) {
continue;
}
// 获取任务
task = std::move(m_taskQueue.front());
m_taskQueue.pop();
if (m_maxQueueSize > 0 && m_taskQueue.size() < m_maxQueueSize) {
m_condFull.notify_one();
}
m_runningTasks++;
}
// 执行任务,传递 stop_token
try {
if (task) {
task(token); // 传递 stop_token 给任务
}
} catch (const std::exception &e) {
std::cerr << "ThreadPool task exception: " << e.what() << std::endl;
} catch (...) {
std::cerr << "ThreadPool unknown task exception" << std::endl;
}
// 更新计数
{
std::lock_guard<std::mutex> lock(m_mutex);
m_runningTasks--;
m_totalTasks--;
// 通知等待的线程
if (m_totalTasks == 0 && m_taskQueue.empty() && m_runningTasks == 0) {
m_condAllDone.notify_all();
}
}
}
}
private:
std::vector<std::unique_ptr<Thread>> m_workers;
std::queue<Task> m_taskQueue;
// 所有状态变量都用 mutex 保护
mutable std::mutex m_mutex;
bool m_stop{false};
size_t m_maxQueueSize;
size_t m_runningTasks{0};
size_t m_totalTasks{0};
std::condition_variable m_condEmpty;
std::condition_variable m_condFull;
std::condition_variable m_condAllDone;
};