-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadPool.h
139 lines (119 loc) · 3.97 KB
/
ThreadPool.h
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
#ifndef THREADPOOL_H_INCLUDED
#define THREADPOOL_H_INCLUDED
/*
Extended from code:
Copyright (c) 2012 Jakob Progsch, Václav Zeman
Modifications:
Copyright (c) 2017-2018 Gian-Carlo Pascutto and contributors
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include <cstddef>
#include <vector>
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <memory>
#include <future>
#include <functional>
namespace Utils {
class ThreadPool {
public:
ThreadPool() = default;
~ThreadPool();
// create worker threads. This version has no initializers.
void initialize(std::size_t);
// add an extra thread. The thread calls initializer() before doing anything,
// so that the user can initialize per-thread data structures before doing work.
void add_thread(std::function<void()> initializer);
template<class F, class... Args>
auto add_task(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>;
private:
std::vector<std::thread> m_threads;
std::queue<std::function<void()>> m_tasks;
std::mutex m_mutex;
std::condition_variable m_condvar;
bool m_exit{false};
};
inline void ThreadPool::add_thread(std::function<void()> initializer) {
m_threads.emplace_back([this, initializer] {
initializer();
for (;;) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(m_mutex);
m_condvar.wait(lock, [this]{ return m_exit || !m_tasks.empty(); });
if (m_exit && m_tasks.empty()) {
return;
}
task = std::move(m_tasks.front());
m_tasks.pop();
}
task();
}
});
}
inline void ThreadPool::initialize(size_t threads) {
for (size_t i = 0; i < threads; i++) {
add_thread([](){} /* null function */);
}
}
template<class F, class... Args>
auto ThreadPool::add_task(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(m_mutex);
m_tasks.emplace([task](){(*task)();});
}
m_condvar.notify_one();
return res;
}
inline ThreadPool::~ThreadPool() {
{
std::unique_lock<std::mutex> lock(m_mutex);
m_exit = true;
}
m_condvar.notify_all();
for (std::thread & worker : m_threads) {
worker.join();
}
}
class ThreadGroup {
public:
ThreadGroup(ThreadPool & pool) : m_pool(pool) {}
template<class F, class... Args>
void add_task(F&& f, Args&&... args) {
m_taskresults.emplace_back(
m_pool.add_task(std::forward<F>(f), std::forward<Args>(args)...)
);
}
void wait_all() {
for (auto && result : m_taskresults) {
result.get();
}
}
private:
ThreadPool & m_pool;
std::vector<std::future<void>> m_taskresults;
};
}
#endif