forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_threadpool.hpp
More file actions
248 lines (194 loc) · 5.44 KB
/
Copy pathsimple_threadpool.hpp
File metadata and controls
248 lines (194 loc) · 5.44 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
// 2018/11/28 - modified by Chun-Xun Lin
//
// Added the method batch to insert a vector of tasks.
//
// 2018/10/04 - modified by Tsung-Wei Huang
//
// Removed shutdown, spawn, and wait_for_all to simplify the design
// of the threadpool. The threadpool now can operates on fixed memory
// closure to improve the performance.
//
// 2018/09/14 - modified by Guannan
// - added wait_for_all method
//
// 2018/04/01 - contributed by Tsung-Wei Huang and Chun-Xun Lin
//
// The basic threadpool implementation based on C++17.
#pragma once
#include <iostream>
#include <functional>
#include <vector>
#include <mutex>
#include <deque>
#include <thread>
#include <stdexcept>
#include <condition_variable>
#include <memory>
#include <future>
#include <unordered_set>
namespace tf {
/**
@class: SimpleThreadpool
@brief Executor that implements a centralized task queue with a simple
execution strategy.
@tparam Closure closure type
*/
template <typename Closure>
class SimpleThreadpool {
public:
/**
@brief constructs the executor with a given number of worker threads
@param N the number of worker threads
*/
explicit SimpleThreadpool(unsigned N);
/**
@brief destructs the executor
Destructing the executor immediately forces all worker threads to stop.
The executor does not guarantee all tasks to finish upon destruction.
*/
~SimpleThreadpool();
/**
@brief constructs the closure in place in the executor
@tparam ArgsT... argument parameter pack
@param args... arguments to forward to the constructor of the closure
*/
template <typename... ArgsT>
void emplace(ArgsT&&... args);
/**
@brief moves a batch of closures to the executor
@param closures a vector of closures to move
*/
void batch(std::vector<Closure>&& closures);
/**
@brief queries the number of worker threads
*/
size_t num_workers() const;
/**
@brief queries if the caller is the owner of the executor
*/
bool is_owner() const;
size_t num_tasks() const;
private:
const std::thread::id _owner {std::this_thread::get_id()};
mutable std::mutex _mutex;
std::condition_variable _worker_signal;
std::vector<Closure> _tasks;
std::vector<std::thread> _threads;
bool _stop {false};
void _spawn(unsigned);
void _shutdown();
};
// Constructor
template <typename Closure>
SimpleThreadpool<Closure>::SimpleThreadpool(unsigned N) {
_spawn(N);
}
// Destructor
template <typename Closure>
SimpleThreadpool<Closure>::~SimpleThreadpool() {
_shutdown();
}
// Function: num_tasks
// Return the number of "unfinished" tasks.
// Notice that this value is not necessary equal to the size of the task_queue
// since the task can be popped out from the task queue while
// not yet finished.
template <typename Closure>
size_t SimpleThreadpool<Closure>::num_tasks() const {
return _tasks.size();
}
template <typename Closure>
size_t SimpleThreadpool<Closure>::num_workers() const {
return _threads.size();
}
// Function: is_owner
template <typename Closure>
bool SimpleThreadpool<Closure>::is_owner() const {
return std::this_thread::get_id() == _owner;
}
// Procedure: spawn
// The procedure spawns "n" threads monitoring the task queue and executing each task.
// After the task is finished, the thread reacts to the returned signal.
template <typename Closure>
void SimpleThreadpool<Closure>::_spawn(unsigned N) {
assert(is_owner());
for(size_t i=0; i<N; ++i) {
_threads.emplace_back([this] () -> void {
Closure task;
std::unique_lock lock(_mutex);
while(!_stop) {
if(!_tasks.empty()) {
task = std::move(_tasks.back());
_tasks.pop_back();
// execute the task
lock.unlock();
task();
lock.lock();
}
else {
while(_tasks.empty() && !_stop) {
_worker_signal.wait(lock);
}
}
} // End of worker loop.
});
}
}
// Function: emplace
template <typename Closure>
template <typename... ArgsT>
void SimpleThreadpool<Closure>::emplace(ArgsT&&... args) {
// No worker, do this right away.
if(num_workers() == 0) {
Closure{std::forward<ArgsT>(args)...}();
}
// Dispatch this to a thread.
else {
std::scoped_lock lock(_mutex);
_tasks.emplace_back(std::forward<ArgsT>(args)...);
_worker_signal.notify_one();
}
}
// Function: emplace
template <typename Closure>
void SimpleThreadpool<Closure>::batch(std::vector<Closure>&& tasks) {
// No worker, do this right away.
if(num_workers() == 0) {
for(auto& t: tasks){
t();
}
return ;
}
// Dispatch this to a thread.
else {
bool notify_all = tasks.size() > 1;
{
std::scoped_lock lock(_mutex);
_tasks.reserve(_tasks.size() + tasks.size());
std::move(tasks.begin(), tasks.end(), std::back_inserter(_tasks));
}
if(notify_all) {
_worker_signal.notify_all();
}
else {
_worker_signal.notify_one();
}
}
}
// Procedure: shutdown
// Shut down the threadpool - only the owner can do this.
template <typename Closure>
void SimpleThreadpool<Closure>::_shutdown() {
assert(is_owner());
{
std::scoped_lock lock(_mutex);
_stop = true;
_worker_signal.notify_all();
}
for(auto& t : _threads) {
t.join();
}
_threads.clear();
_stop = false;
}
}; // end of namespace tf. ---------------------------------------------------