-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_pool_service.h
204 lines (177 loc) · 6.43 KB
/
thread_pool_service.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
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
#ifndef THREAD_POOL_SERVICE_THREAD_POOL_SERVICE_H
#define THREAD_POOL_SERVICE_THREAD_POOL_SERVICE_H
#include <assert.h>
#include <future>
#include "blocking_queue.h"
#include "task_wrapper.h"
#include "thread_wrapper.h"
/**
* Thread pool service, simply implementing some features of ThreadPoolExecutor in java.util.concurrent,
* but with some major differences:
* 1. when constructing the service object, number of {#maximum_pool_size_} threads are started, but only number
* of {#core_pool_size_} threads will do the real work(enabled), the additive thread are disabled and waiting.
* 2. when the {#task_queue_} has be full and the evoke of {BlockingQueue.Push} returns false,
* the {maximum_pool_size - core_pool_size} of disabled threads will be enabled one by one.
* 3. threads newly started are doing the same work with core threads, thus the tasks that failed to submitted
* will still not be executed, but this may pop tasks out of the queue and the tasks came latter may be successfully
* submitted to the queue.
* Also, the {#keep_alive_time} is the maximum idle time of the threads newly started.
*/
class ThreadPoolService {
typedef std::map<std::thread::id, std::shared_ptr<ThreadWrapper> > thread_map;
typedef std::map<std::thread::id, std::shared_ptr<ThreadWrapper> >::value_type thread_type;
public:
ThreadPoolService(int core_pool_size,
int maximum_pool_size,
int keep_alive_time,
BlockingQueue<TaskWrapper> &task_queue)
: finished_(false),
core_pool_size_(core_pool_size),
maximum_pool_size_(maximum_pool_size),
keep_alive_time_(keep_alive_time),
task_queue_(task_queue),
joiner_working_(working_pool_),
joiner_waiting_(waiting_pool_), barrier_(maximum_pool_size_ + 1) {
for (unsigned i = 0; i < core_pool_size_; ++i) {
AddWorker();
}
for (unsigned i = core_pool_size_; i < maximum_pool_size_; ++i) {
AddWaiter();
}
finished_ = false;
barrier_.wait();
assert(ThreadPoolSize() == maximum_pool_size);
std::this_thread::yield();
}
/**
* submit a task to the thread pool service, return a future to get the result later.
*/
template<typename F>
std::future<typename std::result_of<F()>::type> Submit(F f) {
typedef typename std::result_of<F()>::type result_type;
std::packaged_task<result_type()> task(std::move(f));
std::future<result_type> future(task.get_future());
if (Execute(TaskWrapper(std::move(task))))
return future;
else
return (std::future<result_type>());
}
/**
* suspend the thread pool now, submits will be failed and the worker thread will still do the task until the queue is
* empty.
*/
void Shutdown() {
finished_ = true;
barrier_.wait();
}
private:
void DoWork() {
barrier_.wait();
std::shared_ptr<ThreadWrapper> current_worker = GetCurrentThread();
assert(current_worker != nullptr);
while (current_worker->enabled_) {
TaskWrapper task;
if (task_queue_.Pop(task))
task();
else if (finished_)
current_worker->enabled_ = false;
else
std::this_thread::yield();
}
barrier_.wait();
}
void WaitAndWork() {
barrier_.wait();
std::shared_ptr<ThreadWrapper> current_worker = GetCurrentThread();
assert(current_worker != nullptr);
current_worker->WaitToWork();
while (current_worker->enabled_) {
std::shared_ptr<TaskWrapper> task;
if (finished_)
current_worker->enabled_ = false;
else if (GetTask(task)) {
if (task != nullptr)
(*task)();
else
std::this_thread::yield();
} else {
current_worker->StopWorking();
{
std::lock_guard<std::mutex> lg(mutex_);
waiting_pool_.insert(thread_type(std::this_thread::get_id(), current_worker));
working_pool_.erase(std::this_thread::get_id());
}
current_worker->WaitToWork();
}
}
barrier_.wait();
}
bool GetTask(std::shared_ptr<TaskWrapper> &task) {
task = task_queue_.WaitAndPop(keep_alive_time_);
if (task == nullptr && ThreadPoolSize() > core_pool_size_)
return false;
else
return true;
}
bool Execute(TaskWrapper &&task) {
if (finished_)
return false;
if (!task_queue_.Push(std::move(task))) {
AddThread();
return false;
}
return true;
}
void AddThread() {
std::lock_guard<std::mutex> locked_guard(mutex_);
if (working_pool_.size() < maximum_pool_size_ && waiting_pool_.size() > 0) {
std::shared_ptr<ThreadWrapper> &waken_worker = waiting_pool_.begin()->second;
working_pool_.insert(*(waiting_pool_.begin()));
waiting_pool_.erase( waiting_pool_.begin());
waken_worker->StartToWork();
}
}
void AddWorker() {
std::lock_guard<std::mutex> locked_guard(mutex_);
auto boot = std::bind(&ThreadPoolService::DoWork, this);
std::shared_ptr<ThreadWrapper> worker(new ThreadWrapper(boot, true, true));
working_pool_.insert(thread_type(worker->get_id(), worker));
}
void AddWaiter() {
std::lock_guard<std::mutex> locked_guard(mutex_);
auto boot = std::bind(&ThreadPoolService::WaitAndWork, this);
std::shared_ptr<ThreadWrapper> worker(new ThreadWrapper(boot, true, false));
waiting_pool_.insert(thread_type(worker->get_id(), worker));
}
bool CanWork(pool_type &pool) {
std::lock_guard<std::mutex> locked_guard(mutex_);
return pool.find(std::this_thread::get_id()) != pool.end();
}
unsigned ThreadPoolSize() {
std::lock_guard<std::mutex> locked_guard(mutex_);
return working_pool_.size() + waiting_pool_.size();
}
std::shared_ptr<ThreadWrapper> GetCurrentThread() {
std::lock_guard<std::mutex> locked_guard(mutex_);
thread_map::iterator it;
if ((it = working_pool_.find(std::this_thread::get_id())) != working_pool_.end())
return it->second;
else if ((it = waiting_pool_.find(std::this_thread::get_id())) != waiting_pool_.end())
return it->second;
else
return nullptr;
}
std::atomic<bool> finished_;
unsigned core_pool_size_;
unsigned maximum_pool_size_;
unsigned keep_alive_time_;
BlockingQueue<TaskWrapper> &task_queue_;
pool_type working_pool_;
pool_type waiting_pool_;
ThreadsJoiner joiner_working_;
ThreadsJoiner joiner_waiting_;
ThreadBarrier barrier_;
std::mutex mutex_;
std::condition_variable cond_;
};
#endif //THREAD_POOL_SERVICE_THREAD_POOL_SERVICE_H