forked from progschj/ThreadPool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPool.h
More file actions
328 lines (289 loc) · 8.71 KB
/
ThreadPool.h
File metadata and controls
328 lines (289 loc) · 8.71 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
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept>
#include <iostream>
#include <algorithm>
#include <atomic>
// Platform-specific CPU affinity and priority settings
#if defined(_WIN32)
#include <windows.h>
#elif defined(__linux__)
#include <sched.h>
#include <pthread.h>
#endif
class ThreadPool
{
public:
// Thread priority enumeration
enum class Priority
{
LOW,
NORMAL,
HIGH,
REALTIME
};
// Constructor with optional parameters: CPU affinity and priority
ThreadPool(size_t threads,
const std::vector<int>& cpu_affinity = {},
Priority priority = Priority::NORMAL);
// Constructor with numerical priority
ThreadPool(size_t threads,
const std::vector<int>& cpu_affinity,
int custom_priority);
template<class F, class... Args>
auto enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>;
~ThreadPool();
// Wait for all tasks to complete (drain)
void drain();
private:
// Set thread CPU affinity function
void set_thread_affinity(std::thread& thread, int cpu_core);
// Set thread priority function (enumeration version)
void set_thread_priority(std::thread& thread, Priority priority);
// Set thread priority function (numerical version)
void set_thread_priority(std::thread& thread, int custom_priority);
// Thread collection (for joining)
std::vector<std::thread> workers;
// Task queue
std::queue<std::function<void()>> tasks;
// Synchronization primitives
std::mutex queue_mutex;
std::condition_variable condition;
bool stop;
// Stored configurations
std::vector<int> cpu_affinity_;
Priority priority_;
//Task counter and completion condition variable
std::atomic<size_t> task_count_{ 0 }; // Atomic counter for unfinished tasks
std::condition_variable task_done_cond_; // Notification for task completion
};
// Constructor implementation
inline ThreadPool::ThreadPool(size_t threads,
const std::vector<int>& cpu_affinity,
Priority priority)
: stop(false), cpu_affinity_(cpu_affinity), priority_(priority)
{
// Reserve space to avoid reallocation
workers.reserve(threads);
for (size_t i = 0; i < threads; ++i)
{
workers.emplace_back([this, i]
{
for (;;)
{
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock,
[this] { return this->stop || !this->tasks.empty(); });
if (this->stop && this->tasks.empty())
return;
task = std::move(this->tasks.front());
this->tasks.pop();
}
task();
}
});
}
// Set CPU affinity and priority after all threads are created
for (size_t i = 0; i < workers.size(); ++i)
{
// Set CPU affinity (if configured)
if (!cpu_affinity_.empty())
{
int core = cpu_affinity_[i % cpu_affinity_.size()];
set_thread_affinity(workers[i], core);
}
// Set thread priority
set_thread_priority(workers[i], priority_);
}
}
// constructor (numerical priority)
inline ThreadPool::ThreadPool(size_t threads,
const std::vector<int>& cpu_affinity,
int custom_priority)
: stop(false), cpu_affinity_(cpu_affinity)
{
// Reserve space to avoid reallocation
workers.reserve(threads);
for (size_t i = 0; i < threads; ++i)
{
workers.emplace_back([this]
{
for (;;)
{
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock,
[this] { return this->stop || !this->tasks.empty(); });
if (this->stop && this->tasks.empty())
return;
task = std::move(this->tasks.front());
this->tasks.pop();
}
task(); // Execute task
}
});
}
// Set CPU affinity and priority after all threads are created
for (size_t i = 0; i < workers.size(); ++i)
{
// Set CPU affinity (if configured)
if (!cpu_affinity_.empty())
{
int core = cpu_affinity_[i % cpu_affinity_.size()];
set_thread_affinity(workers[i], core);
}
// Set thread priority (numerical version)
set_thread_priority(workers[i], custom_priority);
}
}
// Wait for all tasks to complete (drain)
inline void ThreadPool::drain()
{
std::unique_lock<std::mutex> lock(queue_mutex);
// Wait for task counter to reach zero (efficient waiting via condition variable)
task_done_cond_.wait(lock, [this] { return task_count_ == 0; });
}
// CPU affinity setting implementation (platform-specific)
inline void ThreadPool::set_thread_affinity(std::thread& thread, int cpu_core)
{
#if defined(_WIN32) // Windows implementation
DWORD_PTR mask = static_cast<DWORD_PTR>(1) << cpu_core;
SetThreadAffinityMask(reinterpret_cast<HANDLE>(thread.native_handle()), mask);
#elif defined(__linux__) // Linux implementation
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(cpu_core, &cpuset);
pthread_setaffinity_np(thread.native_handle(), sizeof(cpu_set_t), &cpuset);
#endif
}
// Thread priority setting implementation (platform-specific)
inline void ThreadPool::set_thread_priority(std::thread& thread, Priority priority)
{
#if defined(_WIN32) // Windows implementation
int win_priority;
switch (priority)
{
case Priority::LOW: win_priority = THREAD_PRIORITY_BELOW_NORMAL; break;
case Priority::NORMAL: win_priority = THREAD_PRIORITY_NORMAL; break;
case Priority::HIGH: win_priority = THREAD_PRIORITY_ABOVE_NORMAL; break;
case Priority::REALTIME: win_priority = THREAD_PRIORITY_TIME_CRITICAL; break;
default: win_priority = THREAD_PRIORITY_NORMAL;
}
SetThreadPriority(reinterpret_cast<HANDLE>(thread.native_handle()), win_priority);
#elif defined(__linux__) // Linux implementation
int policy;
struct sched_param param;
// Get current scheduling policy
pthread_getschedparam(thread.native_handle(), &policy, ¶m);
// Set different scheduling policies and priorities based on priority level
switch (priority)
{
case Priority::LOW:
param.sched_priority = sched_get_priority_min(SCHED_OTHER);
fprintf(stderr, "LOW sched_priority:%d\n", sched_get_priority_min(SCHED_OTHER));
break;
case Priority::NORMAL:
// Keep default settings
break;
case Priority::HIGH:
policy = SCHED_RR;
param.sched_priority = (sched_get_priority_min(SCHED_RR) +
sched_get_priority_max(SCHED_RR)) / 2;
fprintf(stderr, "HIGH sched_priority:%d\n", (sched_get_priority_min(SCHED_RR) +
sched_get_priority_max(SCHED_RR)) / 2);
break;
case Priority::REALTIME:
policy = SCHED_FIFO;
param.sched_priority = sched_get_priority_max(SCHED_FIFO);
fprintf(stderr, "REALTIME sched_priority:%d\n", sched_get_priority_max(SCHED_FIFO));
break;
}
// Set scheduling policy
pthread_setschedparam(thread.native_handle(), policy, ¶m);
#endif
}
// Task enqueue (modified: added task counting)
template<class F, class... Args>
auto ThreadPool::enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>
{
using return_type = typename std::result_of<F(Args...)>::type;
auto task = std::make_shared<std::packaged_task<return_type()>>(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);
std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex);
if (stop)
throw std::runtime_error("enqueue on stopped ThreadPool");
// Increase counter when enqueuing
task_count_++;
// Wrap task to decrease counter and notify after execution
tasks.emplace([task, this]()
{
(*task)();
// Decrease counter and notify in a thread-safe manner
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
task_count_--;
if (task_count_ == 0)
{
task_done_cond_.notify_all(); // Notify all waiting drain() calls
}
}
});
}
condition.notify_one();
return res;
}
// Thread priority setting (numerical version)
inline void ThreadPool::set_thread_priority(std::thread& thread, int custom_priority)
{
#if defined(_WIN32)
// Windows priority range: THREAD_PRIORITY_LOWEST(-2) to THREAD_PRIORITY_TIME_CRITICAL(15)
if (custom_priority >= -2 && custom_priority <= 15)
{
SetThreadPriority(reinterpret_cast<HANDLE>(thread.native_handle()), custom_priority);
}
else
{
std::cerr << "Windows: Invalid priority value (range -2~15)" << std::endl;
}
#elif defined(__linux__)
// Linux uses SCHED_RR policy (priority 1~99)
struct sched_param param;
param.sched_priority = custom_priority;
if (custom_priority >= 1 && custom_priority <= 99)
{
pthread_setschedparam(thread.native_handle(), SCHED_RR, ¶m);
}
else
{
std::cerr << "Linux: Invalid priority value (range 1~99)" << std::endl;
}
#endif
}
// Destructor implementation
inline ThreadPool::~ThreadPool()
{
{
std::unique_lock<std::mutex> lock(queue_mutex);
stop = true;
}
condition.notify_all();
for (std::thread& worker : workers)
worker.join();
}
#endif