-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.cpp
More file actions
168 lines (154 loc) · 4.07 KB
/
threadpool.cpp
File metadata and controls
168 lines (154 loc) · 4.07 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
#include "threadpool.hpp"
#include <thread>
#include "Logger.hpp"
#include <iostream>
#include <sstream>
#include <chrono>
#include "result.hpp"
using namespace std;
using namespace placeholders;
const int TASK_MAX_THRESHHOLD = 1024;
const int THREAD_MAX_THRESHHOLD = 200;
threadPool::threadPool():
initThreadSize_(4),
taskSize_(0),
taskQueMaxThreshHold_(TASK_MAX_THRESHHOLD),
mode_(poolMode::POOL_FIXED),
isPoolRunning_(false),
idleThreadNum_(0),
threadMaxThreshHold_(THREAD_MAX_THRESHHOLD),
curThreadNum_(0)
{
}
threadPool::~threadPool(){
isPoolRunning_ = false;
unique_lock<mutex> lock(taskQueMtx_);
notEmpty_.notify_all();
exit_.wait(lock, [&]()->bool {
return threads_.size() == 0;
});
}
//start the thread pool
void threadPool::start(int size)
{
initThreadSize_ = size;
curThreadNum_=initThreadSize_;
isPoolRunning_ = true;
//create Thread class
for (int i = 0; i < initThreadSize_; i++)
{
auto threadptr = make_unique<Thread>(std::bind(&threadPool::threadFunc, this ,_1));
int id = threadptr->getid();
threads_.emplace(id, std::move(threadptr));
}
//start threads
for (int i = 0; i < initThreadSize_; i++)
{
threads_[i]->start();
idleThreadNum_++;
}
}
//submit the task
Result threadPool::submitTask(shared_ptr<Task> task)
{
//lock the taskQue
unique_lock<mutex> lock(taskQueMtx_);
//wait until the size less than the taskQueMaxThreshHold
while (taskQue_.size() >= (size_t)taskQueMaxThreshHold_)
{
if (cv_status::timeout == notFull_.wait_for(lock, std::chrono::seconds(1)))
{
LOG_ERROR("THREADPOOL::SUBMITTASK ERROR time out\n");
return Result(task,false);
}
}
//put into the queue
taskQue_.emplace(task);
taskSize_++;
//notify the consumers
notEmpty_.notify_all();
//if the thread is not enough
if (mode_ == poolMode::POOL_CACHED && taskSize_ > idleThreadNum_ &&
curThreadNum_ < threadMaxThreshHold_)
{
auto threadptr = make_unique<Thread>(std::bind(&threadPool::threadFunc, this,_1));
int id = threadptr->getid();
threads_.emplace(id, std::move(threadptr));
curThreadNum_++;
idleThreadNum_++;//?????
}
return Result(task, true);
}
static int threadIdToInt() {
std::stringstream ss;
ss << std::this_thread::get_id();
int id;
ss >> id;
return id;
}
void threadPool::threadFunc(int threadid)
{
LOG_INFO("THREADPOOL::THREADHANDLER>> TID: %d\n", threadIdToInt());
auto lastTime = std::chrono::high_resolution_clock().now();
//cout << "THREADPOOL::THREADHANDLER>> "<<std::this_thread::get_id() << endl;
while (true)
{
shared_ptr<Task> taskp = nullptr;
{
unique_lock<mutex> lock(taskQueMtx_);
//cached::judge the waiting time
{
while (taskQue_.size() <= 0)
{
if (mode_ == poolMode::POOL_CACHED)
{
if (cv_status::timeout == notEmpty_.wait_for(lock, std::chrono::seconds(1)))
{
auto now = std::chrono::high_resolution_clock().now();
auto dur = std::chrono::duration_cast<std::chrono::seconds>(now - lastTime);
if (dur.count() >= 60 && curThreadNum_ > initThreadSize_)
{
//clean the thread
threads_.erase(threadid);
curThreadNum_--;
idleThreadNum_--;
LOG_INFO("thread %d is cleaned\n", threadid);
return;
}
}
}
//fixed::always wait
else {
notEmpty_.wait(lock);
}
if (isPoolRunning_ == false)
{
threads_.erase(threadid);
LOG_INFO("thread %d is exited\n", threadid);
exit_.notify_all();
return;
}
}
}
idleThreadNum_--;
LOG_INFO("THREADPOOL::THREADHANDLER>> TID: %d\n get the task", threadIdToInt());
taskp = taskQue_.front();
taskQue_.pop();
taskSize_--;
if (taskQue_.size() > 0)
{
notEmpty_.notify_all();
}
notFull_.notify_all();
}
if (taskp) {
taskp->exec();
}
idleThreadNum_++;
lastTime = std::chrono::high_resolution_clock().now();
}
threads_.erase(threadid);
LOG_INFO("thread %d is exited\n", threadid);
exit_.notify_all();
return;
}