-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.hpp
208 lines (147 loc) · 4.11 KB
/
task.hpp
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
#ifndef CPP_TASK_HPP
#define CPP_TASK_HPP
#include <mutex>
#include <condition_variable>
#include <thread>
#include <atomic>
#include <deque>
#include <vector>
#include <functional>
#include <future>
#include <iostream>
// simple work-stealing thread pool based on "better code: concurrency" talk by
// sean parent.
using mutex_type = std::mutex;
using lock_type = std::unique_lock<mutex_type>;
using task_type = std::function< void() >;
class queue {
std::deque<task_type> tasks;
mutex_type mutex;
std::condition_variable cv;
bool stop = false;
lock_type lock() { return lock_type(mutex); }
lock_type try_lock() { return lock_type(mutex, std::try_to_lock); }
public:
void done() {
{
const auto lock = this->lock();
stop = true;
}
cv.notify_all();
}
bool try_pop(task_type& out) {
auto lock = this->try_lock();
if(!lock || tasks.empty()) return false;
out = std::move(tasks.front());
tasks.pop_front();
return true;
}
bool pop(task_type& out) {
auto lock = this->lock();
cv.wait(lock, [this] { return tasks.size() || stop; });
if(tasks.empty()) return false;
out = std::move(tasks.front());
tasks.pop_front();
return true;
};
bool try_push(task_type task) {
{
const auto lock = this->try_lock();
if(!lock) return false;
tasks.emplace_back(std::move(task));
}
cv.notify_one();
return true;
}
void push(task_type task) {
{
const auto lock = this->lock();
tasks.emplace_back(std::move(task));
}
cv.notify_one();
}
};
class pool {
std::atomic<int> last;
std::vector<queue> queues;
std::vector<std::thread> threads;
// how far/long to keep looking for an available thread when pushing task
// (1=try each thread)
const double factor = 1.5;
void run(std::size_t i) {
while(true) {
task_type task;
// try stealing work from someone else's queue (starting from our own)
// instead of waiting for work
for(std::size_t j = 0, n = size(); j < n; ++j) {
if(queues[(i + j) % n].try_pop(task)) break;
}
// no work available: fallback on waiting
if(!task && !queues[i].pop(task)) {
return;
}
task();
}
}
public:
std::size_t size() const { return threads.size(); }
pool(std::size_t n = std::thread::hardware_concurrency())
: last(0),
queues(n) {
for(std::size_t i = 0; i < n; ++i) {
threads.emplace_back([this, i] {
run(i);
});
}
// wait for the threads to start by splitting an empty task
auto join = split(std::size_t(0), n, [](std::size_t) { });
join.get();
}
~pool() {
for(auto& q : queues) {
q.done();
}
for(auto& t : threads) {
t.join();
}
}
void async(task_type task) {
const int next = last++;
// try pushing on a queue that is not locked to avoid contention
for(std::size_t i = 0, n = factor * size(); i < n; ++i) {
if(queues[(next + i) % size()].try_push(std::move(task))) {
return;
}
}
// all the queues are busy: fallback on waiting
queues[next % size()].push(std::move(task));
}
// split a task on each thread
template<class Iterator, class Func>
std::future<void> split(Iterator first, Iterator last, const Func& func) {
const std::size_t n = last - first;
struct shared_type {
std::promise<void> promise;
std::atomic<unsigned> count;
};
auto shared = std::make_shared<shared_type>();
shared->count = size();
const std::size_t m = (n + 1) / size();
Iterator begin = first;
for(std::size_t i = 0; i < size(); ++i) {
const Iterator end = i == size() - 1 ? last : begin + m;
queues[i].push([shared, func, begin, end] {
for(Iterator it = begin; it != end; ++it) {
func(it);
}
// last one fills the promise
if(--shared->count == 0) {
shared->promise.set_value();
}
});
begin += m;
}
return shared->promise.get_future();
}
};
#endif