Repository navigation
Expand file tree
/
Copy pathThreadPool.h
More file actions
151 lines (136 loc) · 4.22 KB
/
Copy pathThreadPool.h
File metadata and controls
151 lines (136 loc) · 4.22 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
#pragma once
#include <cstdint>
#include <iterator>
#include <type_traits>
#include <utility>
#include <vector>
#include <thread>
#include <queue>
#include <functional>
#include <mutex>
#include <condition_variable>
#include <future>
namespace w130205
{
class ThreadPool
{
public:
explicit ThreadPool(std::size_t thread_num) : thdNum(thread_num)
{
thds.reserve(thdNum);
for (size_t i = 0; i < thdNum; ++i) {
thds.emplace_back(&ThreadPool::worker, this);
}
}
ThreadPool(const ThreadPool&) = delete;
ThreadPool(ThreadPool &&) noexcept = delete;
ThreadPool& operator=(const ThreadPool&) = delete;
ThreadPool& operator=(ThreadPool &&) noexcept = delete;
~ThreadPool()
{
release();
}
template<typename F, typename ...Args>
auto submit(F&& f, Args&&... args)
-> std::pair<bool, std::future<std::invoke_result_t<F, Args...>>>
{
using ReturnType = std::invoke_result_t<F, Args...>;
std::packaged_task<ReturnType()> packedTask(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);
std::future<ReturnType> futureRet = packedTask.get_future();
{
std::lock_guard<std::mutex> lg(mtx);
if (stop) {
return { false, std::future<ReturnType>{} };
}
tasks.emplace([packedTask = std::move(packedTask)]() mutable { packedTask(); });
++totalTaskNum;
++remainingTaskNum;
}
cond.notify_one();
return { true, std::move(futureRet) };
}
void release()
{
{
std::lock_guard<std::mutex> lg(mtx);
if (stop) {
return;
}
stop = true;
}
cond.notify_all(); // 置stop,通知所有任务线程,等待剩余任务全部执行完
for (auto &thd : thds) {
if (thd.joinable()) {
thd.join();
}
}
}
private:
void worker()
{
func_wrapper task;
while (true) {
{
std::unique_lock<std::mutex> lk(mtx);
cond.wait(lk,
[this]() {
return !tasks.empty() || stop;
});
if (!tasks.empty()) {
task = std::move(tasks.front());
tasks.pop();
--remainingTaskNum;
} else if (stop) {
return;
}
}
if (task != nullptr) {
task();
}
}
}
class func_wrapper
{
public:
func_wrapper() = default;
func_wrapper(func_wrapper&& other) noexcept : impl(std::move(other.impl)) {};
func_wrapper& operator=(func_wrapper&& other) noexcept
{
impl = std::move(other.impl);
return *this;
}
func_wrapper(const func_wrapper&) = delete;
func_wrapper& operator=(const func_wrapper&) = delete;
template<typename F>
func_wrapper(F&& f)
: impl(std::make_unique<impl_type<std::decay_t<F>>>(std::forward<F>(f))) {}
void operator()() { impl->call(); }
operator bool() { return static_cast<bool>(impl); }
bool operator==(std::nullptr_t) { return impl == nullptr; }
bool operator!=(std::nullptr_t) { return impl != nullptr; }
private:
struct impl_base {
virtual void call() = 0;
virtual ~impl_base() = default;
};
template<typename F>
struct impl_type : impl_base
{
F f;
impl_type(F&& f_) : f(std::forward<F>(f_)) {}
void call() override { f(); }
};
std::unique_ptr<impl_base> impl;
};
bool stop { false };
std::size_t thdNum { 0 };
std::vector<std::thread> thds;
std::queue<func_wrapper> tasks;
std::size_t totalTaskNum { 0 };
std::size_t remainingTaskNum { 0 };
std::mutex mtx;
std::condition_variable cond;
};
} // namespace w130205