-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLfThreadPool.h
More file actions
188 lines (153 loc) · 5.84 KB
/
LfThreadPool.h
File metadata and controls
188 lines (153 loc) · 5.84 KB
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
//
// Created by wanghaitao on 2021/2/25.
//
#ifndef CXXPRACTICE_LFTHREADPOOL_H
#define CXXPRACTICE_LFTHREADPOOL_H
#include <functional>
#include <thread>
#include <atomic>
#include <mutex>
#include <condition_variable>
#include <utility>
namespace yulan {
template<int min_thread_num = 1, int max_thread_num = 10>
class LfThreadPool {
private:
// 记录工作线程状态的工具结构
struct ThreadInfo {
std::thread t;
bool running = false;
};
public:
// 暴露出去的task类
typedef std::function<void(void)> TaskType;
template<typename Fun, typename ...Args>
static inline TaskType make_task(Fun f, Args... args) {
return std::bind(f, args...);
}
public:
// 构造与析构
LfThreadPool(TaskType pre_task, TaskType critical_task, TaskType follow_task);
~LfThreadPool();
LfThreadPool(LfThreadPool &) = delete;
LfThreadPool(LfThreadPool &&) = delete;
LfThreadPool &operator=(LfThreadPool &) = delete;
LfThreadPool &operator=(LfThreadPool &&) = delete;
private:
TaskType pre_task_;
TaskType critical_task_;
TaskType follow_task_;
// 实现管程
std::condition_variable cond_wait; // 管程的条件变量
std::mutex cond_mutex; // 管程的锁
int thread_num_allow_in;
std::vector<ThreadInfo> threads; // worker threads
std::thread scheduler; // scheduler thread
bool shutdown;
// 线程池运行状态记录
int busy_thread_num;
int live_thread_num;
int wait_for_exit_num;
inline void set_thread(int pool_index) {
threads[pool_index].t = std::thread(
make_task(&LfThreadPool<min_thread_num, max_thread_num>::worker_func, this, pool_index));
threads[pool_index].running = true;
}
void worker_func(int pool_index);
void scheduler_func();
};
template<int min_thread_num, int max_thread_num>
LfThreadPool<min_thread_num, max_thread_num>::LfThreadPool(TaskType pre_task,
TaskType critical_task,
TaskType follow_task):
pre_task_(std::move(pre_task)),
critical_task_(std::move(critical_task)),
follow_task_(std::move(follow_task)),
thread_num_allow_in(1),
threads(max_thread_num),
scheduler(&LfThreadPool<min_thread_num, max_thread_num>::scheduler_func, this),
shutdown(false),
busy_thread_num(min_thread_num),
live_thread_num(min_thread_num),
wait_for_exit_num(0) {
for (int i = 0; i < min_thread_num; ++i) {
set_thread(i);
}
}
template<int min_thread_num, int max_thread_num>
void LfThreadPool<min_thread_num, max_thread_num>::worker_func(int pool_index) {
for (;;) {
// 前置任务
pre_task_();
{
// 进入临界区
std::unique_lock<std::mutex> lk(cond_mutex);
--busy_thread_num;
while (thread_num_allow_in <= 0)
cond_wait.wait(lk);
// 取得执行权
--thread_num_allow_in;
if (wait_for_exit_num > 0) {
--wait_for_exit_num;
threads[pool_index].t.detach(); // 自我了断
threads[pool_index].running = false;
--live_thread_num;
break;
}
// 临界任务
critical_task_();
// 退出临界区
++busy_thread_num;
++thread_num_allow_in;
}
cond_wait.notify_one(); // 先解锁后唤醒
// 后续非临界处理任务
follow_task_();
}
}
template<int min_thread_num, int max_thread_num>
void LfThreadPool<min_thread_num, max_thread_num>::scheduler_func() {
while (!shutdown) {
{
std::lock_guard<std::mutex> lk(cond_mutex);
// 扩充线程
if (busy_thread_num * 5 > live_thread_num * 4) {
int add_num = busy_thread_num / 4;
for (int i = 0; i < max_thread_num && add_num > 0 && live_thread_num < max_thread_num; ++i) {
if (!threads[i].running) {
set_thread(i);
++live_thread_num;
--add_num;
}
}
}
// 减少线程
else if (busy_thread_num * 2 < live_thread_num) {
int quit_num = std::min(busy_thread_num, live_thread_num - min_thread_num);
wait_for_exit_num = quit_num;
thread_num_allow_in += quit_num;
cond_wait.notify_all();
}
} // std::lock_guard<std::mutex> lk(cond_mutex)
std::this_thread::yield();
}
}
template<int min_thread_num, int max_thread_num>
LfThreadPool<min_thread_num, max_thread_num>::~LfThreadPool() {
shutdown = true;
scheduler.join();
// scheduler 已经退出,live_thread_num 不会改变
{
std::lock_guard<std::mutex> lk(cond_mutex);
wait_for_exit_num = live_thread_num;
thread_num_allow_in += wait_for_exit_num;
}
cond_wait.notify_all();
for (int i = 0; i < max_thread_num; ++i) {
if (threads[i].running) {
threads[i].t.join();
}
}
}
} // namespace yulan
#endif //CXXPRACTICE_LFTHREADPOOL_H