-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththread_pool
330 lines (270 loc) · 11.9 KB
/
thread_pool
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
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_EXECUTION_THREAD_POOL_HPP
#define GTL_EXECUTION_THREAD_POOL_HPP
// Summary: Multi-queue thread-pool that performs jobs in priority order.
#ifndef NDEBUG
# if defined(_MSC_VER)
# define __builtin_trap() __debugbreak()
# endif
/// @brief A simple assert macro to break the program if the thread_pool is misused.
# define GTL_THREAD_POOL_ASSERT(ASSERTION, MESSAGE) static_cast<void>((ASSERTION) || (__builtin_trap(), 0))
#else
/// @brief At release time the assert macro is implemented as a nop.
# define GTL_THREAD_POOL_ASSERT(ASSERTION, MESSAGE) static_cast<void>(0)
#endif
#if defined(_MSC_VER)
# pragma warning(push, 0)
#endif
#include <atomic>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <queue>
#include <set>
#include <thread>
#include <vector>
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
namespace gtl {
/// @brief The thread_pool class implements a pool of threads that process tasks from queues in priority order.
class thread_pool final {
public:
/// @brief The queue class implements a queue of tasks that are processed by a thread_pool.
class queue final {
private:
friend class thread_pool;
private:
/// @brief A comparison structure to enable standard containers to order queue objects by priority.
struct comparison final {
bool operator()(const queue* lhs, const queue* rhs) const {
return lhs->priority < rhs->priority;
}
};
private:
/// @brief Reference to the thread_pool that this queue is being processed by.
thread_pool& pool;
/// @brief The priority of the tasks in this queue, lower value is higher priority.
int priority;
/// @brief The number of tasks added to this queue.
unsigned int inserted;
/// @brief The number of tasks completed from this queue.
std::atomic<unsigned int> completed;
/// @brief Mutex to control access to the queue of tasks.
mutable std::mutex tasks_mutex;
/// @brief The queue of tasks.
std::queue<std::function<void()>> tasks;
public:
/// @brief Destructor performs debug checks to make sure the queue is not misused.
~queue() {
GTL_THREAD_POOL_ASSERT(this->empty(), "Thread pool queue still contains pending tasks.");
GTL_THREAD_POOL_ASSERT(this->finished(), "Thread pool queue is still being processed.");
}
/// @brief Constructor that sets the reference to the thread_pool and initialises internal variables.
/// @param target_pool The thread_pool that will process the tasks in this queue.
/// @param queue_priority The priority of the tasks in this queue, lower value is higher priority.
queue(thread_pool& target_pool, int queue_priority = 0)
: pool(target_pool)
, priority(queue_priority)
, inserted(0)
, completed(0) {
}
public:
/// @brief Add a task to this queue.
/// @param task The task to add.
void push(const std::function<void()>& task);
/// @brief Block until all tasks in this queue have been completed by the thread_pool.
void drain();
/// @brief Check if the queue is empty.
/// @return true if the queue of tasks is empty, false otherwise.
bool empty() const {
std::lock_guard<std::mutex> lock(this->tasks_mutex);
return this->tasks.empty();
}
/// @brief Check if all tasks inserted into the queue have been completed.
/// @return true if all tasks have been completed, false otherwise.
bool finished() const {
std::lock_guard<std::mutex> lock(this->tasks_mutex);
return (this->inserted == this->completed);
}
};
private:
/// @brief Flag that specifies if the interal threads should sleep or exit when there are no queues to process.
bool running;
/// @brief The array of internal threads.
std::vector<std::thread> threads;
/// @brief Mutex to control access to the set of queues.
std::mutex queue_mutex;
/// @brief Condition variable to allow the internal threads to sleep when no queues are available.
std::condition_variable queue_available;
/// @brief Set of queues to process, queues are removed when empty of tasks.
std::set<queue*, queue::comparison> queues;
public:
/// @brief Destructor performs debug checks to make sure the thread_pool is not misused.
~thread_pool() {
// Ensure the thread_pool has been joined.
GTL_THREAD_POOL_ASSERT(!this->joinable(), "Thread pool is still joinable.");
}
/// @brief Constructor that allocates the internal threads and starts them running.
/// @param thread_count The number of threads to use.
thread_pool(unsigned int thread_count = std::max(std::thread::hardware_concurrency(), 1u) - 1u)
: running(true) {
this->threads.reserve(thread_count);
for (unsigned int thread_index = 0; thread_index < thread_count; ++ thread_index) {
this->threads.emplace_back(&thread_pool::thread_loop, this);
}
}
/// @brief Deleted copy constructor.
thread_pool(const thread_pool&) = delete;
/// @brief Deleted move constructor.
thread_pool(thread_pool&&) = delete;
/// @brief Deleted copy assignment operator.
thread_pool& operator=(const thread_pool&) = delete;
/// @brief Deleted move assignment operator.
thread_pool& operator=(thread_pool&&) = delete;
private:
/// @brief The core loop that is run on each thread_pool thread.
void thread_loop() {
queue* queue = nullptr;
std::function<void()> task;
for (;;) {
// Try and pop a task from the highest priority queue.
{
// First check if there are any queues.
std::lock_guard<std::mutex> lock(this->queue_mutex);
if (!this->queues.empty()) {
// Select the highest priority queue.
queue = *this->queues.begin();
{
std::lock_guard<std::mutex> lock2(queue->tasks_mutex);
// Try and get a task.
if (!queue->tasks.empty()) {
task = std::move(queue->tasks.front());
queue->tasks.pop();
}
// If there's no tasks left on this queue, remove it.
else {
this->queues.erase(queue);
}
}
}
}
// If this thread got a job.
if (task) {
task();
++queue->completed;
}
// Otherwise, wait for more work and potentially exit.
else {
std::unique_lock<std::mutex> lock(this->queue_mutex);
// Wait for more work, or exit signal.
this->queue_available.wait(lock, [&]{ return !this->running || !this->queues.empty(); });
// Check for exit.
if (!this->running && this->queues.empty()) {
break;
}
}
// Clear the task.
task = nullptr;
}
}
public:
/// @brief Block until all tasks in a queue have been completed by the thread_pool.
/// @param queue The queue of tasks to empty.
void drain(queue& queue) {
std::function<void()> task;
for (;;) {
// Try and pop a task from the queue.
{
std::lock_guard<std::mutex> lock(this->queue_mutex);
{
std::lock_guard<std::mutex> lock2(queue.tasks_mutex);
if (!queue.tasks.empty()) {
task = std::move(queue.tasks.front());
queue.tasks.pop();
}
}
}
// If this thread got a task.
if (task) {
task();
++queue.completed;
// Clear the task.
task = nullptr;
}
// Otherwise there are no tasks left, so break out of the loop.
else {
break;
}
}
// Wait for all working threads to finish.
while (!queue.finished()) {
std::this_thread::yield();
}
}
public:
/// @brief Check if the thread_pool threads are joinable.
/// @return true if the threads are joinable, false otherwise.
bool joinable() const {
// Check if any of the threads are joinable.
for (const std::thread& thread : this->threads) {
if (thread.joinable()) {
return true;
}
}
return false;
}
/// @brief Block until all queues in the thread_pool are empty, then join all threads.
void join() {
// Stop pool and wake threads.
{
std::lock_guard<std::mutex> lock(this->queue_mutex);
this->running = false;
this->queue_available.notify_all();
}
// Ensure work is finished.
this->thread_loop();
// Join threads.
for (std::thread& thread : this->threads) {
if (thread.joinable()) {
thread.join();
}
}
}
};
// The push function for the queue class is implemented here as it needs to access the thread_pool class.
inline void thread_pool::queue::push(const std::function<void()>& task) {
{
// Add the task to the queue.
std::lock_guard<std::mutex> lock(this->tasks_mutex);
++this->inserted;
this->tasks.push(task);
}
{
// Ensure the queue is live in the pool.
std::lock_guard<std::mutex> lock(this->pool.queue_mutex);
// WARNING: This line triggers a "use-of-uninitialized-value" in MemorySanitizer, but is a false positive.
this->pool.queues.emplace(this);
// Notify a thread in the pool that there is a queue available.
this->pool.queue_available.notify_one();
}
}
// The drain function for the queue class is implemented here as it needs to access the thread_pool class.
inline void thread_pool::queue::drain() {
this->pool.drain(*this);
}
}
#undef GTL_THREAD_POOL_ASSERT
#endif // GTL_EXECUTION_THREAD_POOL_HPP