forked from lpcvoid/cpp-net-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_pool.hpp
More file actions
121 lines (113 loc) · 3.93 KB
/
Copy paththread_pool.hpp
File metadata and controls
121 lines (113 loc) · 3.93 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
#pragma once
#include <condition_variable>
#include <functional>
#include <future>
#include <queue>
#include <thread>
#include <vector>
namespace netlib {
class thread_pool {
private:
std::vector<std::thread> _thread_pool;
std::condition_variable _cv_new_job;
std::mutex _mutex;
std::queue<std::function<void()>> _task_queue;
std::atomic<bool> _active = false;
std::size_t _max_threads = 1;
void add_thread() {
auto thread_worker_fun = [this](){
while (_active) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(_mutex);
_cv_new_job.wait(lock, [this] {
//we wait either until there is at least one task
//in the queue, or until we shutdown this thing
return (!_task_queue.empty()) || (!_active);
});
if (!_active) {
break; //terminate thread
}
task = std::move(_task_queue.front());
_task_queue.pop();
}
//actually execute the task
task();
}
};
_thread_pool.emplace_back(std::thread(thread_worker_fun));
}
void create_threads(std::size_t thread_count) {
_thread_pool.reserve(thread_count + _thread_pool.size());
for (uint32_t i = 0; i < thread_count; ++i) {
add_thread();
}
}
private:
thread_pool(std::size_t start_threads, std::size_t max_threads) {
_active = true;
_max_threads = max_threads;
create_threads(start_threads);
}
public:
thread_pool(){
_active = true;
_max_threads = std::thread::hardware_concurrency();
create_threads(1);
}
//only way to influence the max threads is via template
//instantiation, since we can assert correctness at compile time
template<typename std::size_t StartT, typename std::size_t MaxT>
static thread_pool create() {
static_assert(MaxT > 0, "Max threads shall be positive");
static_assert((StartT <= MaxT), "Max threads shall be higher or equal to the starting thread count");
return {StartT, MaxT};
}
~thread_pool(){
_active = false;
_cv_new_job.notify_all();
for (auto& thread : _thread_pool){
thread.join();
}
}
std::size_t get_thread_count() {
return _thread_pool.size();
}
std::size_t get_task_count() {
std::lock_guard<std::mutex> lock(_mutex);
return _task_queue.size();
}
//https://stackoverflow.com/a/31078143
template <typename FUNCTION, typename... FUNCARGS>
std::future<typename std::result_of<FUNCTION(FUNCARGS...)>::type>
add_task(FUNCTION&& function, FUNCARGS&&... args) {
// first, check if we even have a free thread
// if not, we add one up to a max allowed
std::size_t free_threads = (get_thread_count() - get_task_count());
if (!free_threads) {
if (_thread_pool.size() < _max_threads) {
create_threads(1);
}
}
// we need the return type of the task we are given (C++17)
using return_type = std::invoke_result_t<FUNCTION, FUNCARGS...>;
// crate ptr which we then use to get a future later
auto task = std::make_shared< std::packaged_task<return_type()> > (
// execute function with provided arg reference via forward call wrapper
std::bind(std::forward<FUNCTION>(function), std::forward<FUNCARGS>(args)...));
{
std::unique_lock<std::mutex> lock(_mutex);
//we actually emplace a lambda which then executes the function with args
//we got passed here. Function is encoded in the std::packaged_task
_task_queue.emplace([task]() -> void {
//deref shared_ptr and execute function
(*task)();
});
}
//notify a waiting thread that there's a new task
_cv_new_job.notify_one();
//call get_future of the packaged_task
return task->get_future();
}
};
}