-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadpool.h
185 lines (149 loc) · 3.42 KB
/
Threadpool.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
#pragma once
#include <vector>
#include<memory>
#include<queue>
#include <atomic>
#include <mutex>
#include <condition_variable>
#include<functional>
#include<unordered_map>
class Any
{
public:
template<typename T>
Any(T data) :base_(std::make_unique<Derive<T>>(data)) {}
template<typename T>
T cast_()
{
Derive<T>* pd = dynamic_cast<Derive<T>*>(base_.get());
return pd->data_;
}
Any() = default;
~Any() = default;
Any(const Any&) = delete;
Any& operator=(const Any&) = delete;
Any(Any&&) = default;
Any& operator=(Any&&) = default;
private:
class Base
{
public:
virtual ~Base() = default;
};
template<typename T>
class Derive : public Base
{
public:
Derive(T data) :data_(data) {}
T data_;
};
std::unique_ptr<Base> base_;
};
class Semaphore
{
public:
Semaphore(int limit = 0)
:resLimit(limit)
,isExit_(false)
{}
~Semaphore()
{
isExit_ = true;
}
void wait()
{
if (isExit_) return;
std::unique_lock<std::mutex> lock(mtx_);
cond_.wait(lock, [&]()->bool {return resLimit > 0; });
resLimit--;
}
void post()
{
if (isExit_) return;
std::unique_lock<std::mutex> lock(mtx_);
resLimit++;
cond_.notify_all();
}
private:
int resLimit;
std::mutex mtx_;
std::condition_variable cond_;
std::atomic_bool isExit_;
};
class Task;
class Result
{
public:
Result(std::shared_ptr<Task> task, bool isValid = true);
~Result() = default;
//用户调用->获取返回值
Any get();
//任务执行完调用,获取任务执行完的返回值,为成员变量any_赋值
void setVal(Any any);
private:
Any any_;
std::shared_ptr<Task> task_;
Semaphore sem_;
std::atomic_bool isValid_;
};
enum class PoolMode {
MODE_FIXED,
MODE_CACHED
};
class Task
{
public:
Task();
~Task() = default;
virtual Any run() = 0;
void exec();
void setResult(Result* res);
private:
Result* result_; //防止强智能指针交叉引用
};
//线程类型
class Thread
{
public:
using ThreadFunc = std::function<void(int)>;
Thread(ThreadFunc func);
~Thread();
//启动线程
void start();
int getID() const;
private:
ThreadFunc func_;
static int generateId_;
int threadId_;
};
//线程池类型
class ThreadPool
{
public:
ThreadPool();
~ThreadPool();
ThreadPool(const ThreadPool&) = delete;
ThreadPool& operator=(const Thread&) = delete;
void setMode(PoolMode mode);
void setTaskQueMaxThreshHold(int threshhold);
Result submitTask(std::shared_ptr<Task> sp);//提交任务
void start(int initThreadSize = 4); //线程池启动
private:
void threadFunc(int threadid);
bool checkRunningState() const;
//std::vector<std::unique_ptr<Thread>> threads_; //线程列表
std::unordered_map<int, std::unique_ptr<Thread>> threads_;
int initThreadSize_; //初始的线程数量
std::atomic_int idleThreadSize_; //空闲线程数量
std::atomic_int curThreadSize_; //当前线程数量
int threadSizeThreshHold_; //线程数量上限阈值
std::queue<std::shared_ptr<Task>> taskQue_;//任务队列
std::atomic_int taskSize_; //原子类型
int taskQueMaxThreshHold_; //任务上限阈值
std::mutex taskQueMtx_; //负责任务队列的安全
std::condition_variable notFull_;
std::condition_variable notEmpty_;
std::condition_variable exitCond_; //资源回收控制
PoolMode poolMode_;
std::atomic_bool ispoolrunning_;
};