-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.hpp
More file actions
77 lines (62 loc) · 1.71 KB
/
threadpool.hpp
File metadata and controls
77 lines (62 loc) · 1.71 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
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <vector>
#include <memory>
#include <atomic>
#include <mutex>
#include <queue>
#include <condition_variable>
#include "Thread.hpp"
#include "Task.hpp"
#include <unordered_map>
#include <thread>
using namespace std;
enum class poolMode {
POOL_FIXED,//fixed mode based on the cpu cores;
POOL_CACHED//flexible mode dynamic increment
};
class threadPool
{
public:
threadPool();
~threadPool();
//set the working mode of the thread pool
void setMode(poolMode mode) {
if (checkRunningState())return;
mode_ = mode; }
//start the thread pool
void start(int size=std::thread::hardware_concurrency());
//set the working taskqueue threshhold
void setTaskQueThreshHold(int threshhold) { taskQueMaxThreshHold_ = threshhold; }
void setThreadThreshHold(int threshhold) {
if (checkRunningState())return;
if (mode_ == poolMode::POOL_FIXED) {
threadMaxThreshHold_ = threshhold;
}
}
//submit the task
Result submitTask(shared_ptr<Task> task);
void threadFunc(int );
threadPool(const threadPool& cp) = delete;
threadPool& operator=(const threadPool& cp) = delete;
private:
bool checkRunningState() {
return isPoolRunning_;
}
//vector<unique_ptr<Thread>> threads_;
unordered_map<int, unique_ptr<Thread>> threads_;
int initThreadSize_;
std::atomic_int idleThreadNum_;
int threadMaxThreshHold_;
std::atomic_int curThreadNum_;
queue<shared_ptr<Task>> taskQue_;
atomic_int taskSize_;
int taskQueMaxThreshHold_;
mutex taskQueMtx_;
std::condition_variable notEmpty_;
std::condition_variable notFull_;
std::condition_variable exit_;
poolMode mode_;
std::atomic_bool isPoolRunning_;
};
#endif