-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_pool.h
More file actions
133 lines (110 loc) · 3.81 KB
/
thread_pool.h
File metadata and controls
133 lines (110 loc) · 3.81 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
//
// Created by 86137 on 2022/9/13.
//
#ifndef ASYNC_IO_THREAD_POOL_H
#define ASYNC_IO_THREAD_POOL_H
#include <thread>
#include <memory>
#include <vector>
#include <functional>
#include <exception>
#include "block_queue.h"
namespace async {
template<class CallBack>
concept void_result_callback = requires(CallBack c) { c(std::exception_ptr{}); };
template<class R, class CallBack>
concept result_callback = requires(std::optional<R> res, CallBack c) { c(std::exception_ptr{}, res); };
template<class Func, class CallBack>
struct thread_pool_callback {
static constexpr bool value = (std::invocable<Func> && (
(std::is_same_v<std::invoke_result<Func>, void> && void_result_callback<CallBack>) ||
(!std::is_same_v<std::invoke_result<Func>, void> && result_callback<std::invoke_result<Func>, CallBack>)
));
};
class thread_pool {
using Task = std::function<void()>;
public:
struct default_call_back {
void operator()(std::exception_ptr ptr, auto&& res)const {
if (ptr)
std::rethrow_exception(ptr);
}
void operator()(std::exception_ptr ptr)const {
if (ptr)
std::rethrow_exception(ptr);
}
};
explicit thread_pool(int x) {
for (auto i{ 0 }; i < x; ++i) {
_threads.emplace_back([this] {
while (!_stop) {
auto task = _queue.pop();
if (task)
(*task)();
}
});
}
}
thread_pool(const thread_pool&) = delete;
thread_pool& operator=(const thread_pool&) = delete;
~thread_pool() {
if (!_stop)
stop();
for (auto& t : _threads)
if (t.joinable())
t.join();
}
template<class Func, class CallBack>
// requires thread_pool_callback<Func, CallBack>::value
auto post(Func&& func, CallBack&& call_back) {
_queue.push([task = std::forward<Func>(func),
call_back = std::forward<CallBack>(call_back)] {
using result_type = std::invoke_result_t<Func>;
if constexpr (std::is_same<result_type, void>::value) {
std::exception_ptr ptr;
try {
task();
}
catch (...) {
ptr = std::current_exception();
}
call_back(ptr);
}
else {
std::exception_ptr ptr;
std::optional<result_type> res;
try {
res = task();
}
catch (...) {
ptr = std::current_exception();
res = std::nullopt;
}
call_back(ptr, std::move(res));
}
});
}
template<class Func>
requires std::invocable<Func>
auto post(Func&& func) {
return post(std::forward<Func>(func), default_call_back{});
}
void stop() {
auto counter = std::make_shared<std::atomic_int64_t>(_threads.size() + 1);
for (auto i{ 0 }; i < _threads.size(); ++i) {
_queue.push([counter] {
counter->fetch_sub(1);
while (*counter != 0);
});
}
while (*counter != 1);
_stop.store(true);
counter->fetch_sub(1);
}
private:
std::vector<std::thread> _threads;
async::block_queue<Task> _queue;
std::atomic_bool _stop{ false };
};
}
#endif //ASYNC_IO_THREAD_POOL_H