-
Notifications
You must be signed in to change notification settings - Fork 4
/
thread_pool.cpp
173 lines (145 loc) · 3.77 KB
/
thread_pool.cpp
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
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <list>
#include <atomic>
#include <thread>
#include <functional>
using namespace std;
template<typename T>
class SyncQueue {
public:
SyncQueue(int capacity) : capacity_(capacity), stoping_(false) {
}
void Put(const T& x) {
Add(x);
}
void Put(T&& x) {
Add(std::forward<T>(x));
}
void Take(std::list<T>& list) {
std::unique_lock<std::mutex> locker(mutex_);
while (!stoping_ && queue_.empty()) {
not_empty_.wait(locker);
}
if (stoping_) {
return;
}
list = std::move(queue_);
not_full_.notify_one();
}
void Take(T& t) {
std::unique_lock<std::mutex> locker(mutex_);
while (!stoping_ && queue_.empty()) {
not_empty_.wait(locker);
}
if (stoping_) {
return;
}
t = queue_.front();
queue_.pop_front();
not_full_.notify_one();
}
bool Empty() {
std::lock_guard<std::mutex> locker(mutex_);
return queue_.empty();
}
bool Full() {
std::lock_guard<std::mutex> locker(mutex_);
return queue_.size() == capacity_;
}
void Stop() {
{
std::lock_guard<std::mutex> locker(mutex_);
stoping_ = true;
}
not_empty_.notify_all();
not_full_.notify_all();
}
private:
template<typename F>
void Add(F&& x) {
std::unique_lock<std::mutex> locker(mutex_); //lock
while (!stoping_ && queue_.size() >= capacity_) {
not_full_.wait(locker); //unlock & lock
}
if (stoping_) {
return;
}
queue_.emplace_back(std::forward<F>(x));
not_empty_.notify_one();
}
private:
std::mutex mutex_;
std::condition_variable not_full_;
std::condition_variable not_empty_;
int capacity_;
bool stoping_;
std::list<T> queue_;
};
class ThreadPool {
private:
using Task = std::function<void()>;
public:
ThreadPool(int thread_numbers = std::thread::hardware_concurrency(), int capacity = 100)
: queue_(capacity) {
Start(thread_numbers);
}
~ThreadPool() {
Stop();
}
void Stop() {
std::call_once(once_flag_, [this] { StopThreads(); });
}
void AddTask(const Task& task) {
queue_.Put(task);
}
void AddTask(Task&& task) {
queue_.Put(std::forward<Task>(task));
}
private:
void Start(int thread_numbers) {
running_ = true;
for (int i = 0; i < thread_numbers; ++i) {
thread_list_.emplace_back(std::make_shared<std::thread>(std::bind(&ThreadPool::Run, this)));
}
}
void Run() {
while (running_) {
std::list<Task> task_list;
queue_.Take(task_list);
for (auto& task : task_list) {
if (!running_) {
return;
}
task();
}
}
}
void StopThreads() {
queue_.Stop();
running_ = false;
for (auto thd : thread_list_) {//shared_ptr
if(thd) {
thd->join();
}
}
thread_list_.clear();
}
private:
SyncQueue<Task> queue_;
std::list<std::shared_ptr<std::thread>> thread_list_;
std::atomic_bool running_;
std::once_flag once_flag_;
};
int main(int argc, char const *argv[]) {
ThreadPool pool;
pool.AddTask([] { std::cout << "hello "; });
pool.AddTask([] { std::cout << "world!" << std::endl; });//flush
auto task1 = [](int value) { std::cout << value << std::endl; };
int value = 123; //Parameter passing
auto task2 = std::bind(task1, value);
pool.AddTask(task2);
getchar();
return 0;
}