-
Notifications
You must be signed in to change notification settings - Fork 4
/
pool.h
149 lines (131 loc) · 4.07 KB
/
pool.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
140
141
142
143
144
145
146
147
148
149
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#include <vector>
#include <queue>
#include <list>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <memory>
#include <functional>
namespace Thread {
namespace Safe {
template<typename T>
class Queue {
public:
Queue() : _queue(), qutex(), cond(), _size(0) {}
virtual ~Queue() {}
virtual void push(const T& element) {
{
std::unique_lock<std::mutex> lock(qutex);
_push(element);
cond.notify_all();
}
}
virtual const T& pop() {
T& ret = nullptr;
{
std::unique_lock<std::mutex> lock(qutex);
wait(lock);
ret = _pop();
}
return ret;
}
size_t size() const { return _size; }
protected:
void wait(std::unique_lock<std::mutex>& lock) {
while (_queue.empty())
cond.wait(lock);
}
void _push(const T& element) {
_queue.push_back(element);
++_size;
}
T& _pop() {
if (!_queue.empty()) {
T& ret = _queue.front();
_queue.pop_front();
--_size;
return ret;
}
return nullptr;
}
std::list<T> _queue;
std::mutex qutex;
std::condition_variable cond;
size_t _size;
};
}
class Pool {
public:
Pool(size_t);
~Pool();
void wait() {
{
std::unique_lock<std::mutex> lock(qutex);
cond.wait(lock, [this]() -> bool {
return tasks.empty();
});
}
}
size_t size() { return _size; }
template <class F, class... Args>
auto push(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>;
private:
std::vector<std::thread> workers;
std::queue<std::function<void()>> tasks;
std::mutex qutex;
std::condition_variable cond;
size_t _size;
bool stop;
};
inline Pool::Pool(size_t size) : _size(size), stop(false) {
for (size_t i = 0; i < size; ++i)
workers.emplace_back([this]() {
for(;;) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->qutex);
this->cond.wait(lock, [this]() -> bool {
return this->stop || !this->tasks.empty();
});
if (this->stop && this->tasks.empty())
return;
task = std::move(this->tasks.front());
this->tasks.pop();
}
task();
}
}
);
}
template <class F, class... Args>
auto Pool::push(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(qutex);
tasks.emplace([task]() {
(*task)();
});
}
cond.notify_one();
return res;
}
inline Pool::~Pool() {
{
std::unique_lock<std::mutex> lock(qutex);
stop = true;
}
cond.notify_all();
for (auto& worker: workers)
worker.join();
}
}
#endif