-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathThreadPool.cpp
More file actions
130 lines (115 loc) · 3.63 KB
/
Copy pathThreadPool.cpp
File metadata and controls
130 lines (115 loc) · 3.63 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
/*************************************************************************
> File Name: ThreadPool.cpp
> Author: zhangfeng
> Mail: brave_zephyr@163.com
> Created Time: Wed 16 Oct 2019 04:03:05 PM CST
> Target:
************************************************************************/
#include "ThreadPool.h"
const int time_wait = 5;
void* StartAdjust(void *arg) {
ThreadPool *pool = static_cast<ThreadPool *>(arg);
pool->AdjustThread();
}
ThreadPool::ThreadPool(unsigned int minThreadNum, unsigned int maxThreadNum):
shutDown_(false),
livThreadNum_(0),
minThreadNum_(minThreadNum),
maxThreadNum_(maxThreadNum),
taskSize_(0),
busyThreadNum_(0)
{
pthread_mutex_init(&mutex_, nullptr);
pthread_mutex_init(&taskMutex_, nullptr);
pthread_cond_init(&cond_, nullptr);
pthread_mutex_init(&counterMutex_, nullptr);
}
ThreadPool::~ThreadPool() {
if(taskSize_ > 0) {
for(int i = 0; i < time_wait; i++) {
sleep(1);
std::cout << "taskSize: " << TaskSize() << ", " << time_wait - i << "s will shut!\n";
}
}
Destroy();
pthread_cond_destroy(&cond_);
pthread_mutex_lock(&mutex_);
pthread_mutex_destroy(&mutex_);
while(!taskQueue_.empty()) {
taskQueue_.pop();
}
while(!threadVector_.empty()) {
threadVector_.pop_back();
}
}
unsigned int ThreadPool::TaskSize() {
return taskSize_;
}
std::shared_ptr<ThreadTask> ThreadPool::GetThreadTask() {
pthread_mutex_lock(&taskMutex_);
if(!taskQueue_.empty()) {
auto task = taskQueue_.front();
taskQueue_.pop();
taskSize_--;
pthread_mutex_unlock(&taskMutex_);
return task;
}
pthread_mutex_unlock(&taskMutex_);
return nullptr;
}
void ThreadPool::IncBusy() {
pthread_mutex_lock(&counterMutex_);
busyThreadNum_++;
pthread_mutex_unlock(&counterMutex_);
}
void ThreadPool::DscBusy() {
pthread_mutex_lock(&counterMutex_);
busyThreadNum_--;
pthread_mutex_unlock(&counterMutex_);
}
void ThreadPool::TaskAdd(std::shared_ptr<ThreadTask> threadTask) {
pthread_mutex_lock(&taskMutex_);
taskQueue_.push(threadTask);
taskSize_++;
pthread_mutex_unlock(&taskMutex_);
pthread_cond_broadcast(&cond_);
}
void ThreadPool::Destroy() {
for(auto thread : threadVector_) {
thread->Shut();
}
}
void ThreadPool::Run() {
for(int i = 0; i < minThreadNum_; i++) {
std::shared_ptr<Thread> thread(new Thread(&cond_, &mutex_, std::shared_ptr<ThreadPool>(this)));
thread->Start();
threadVector_.push_back(thread);
livThreadNum_++;
}
pthread_t pid2;
pthread_create(&pid2, nullptr, StartAdjust, static_cast<void*>(this));
}
void ThreadPool::AdjustThread() {
while(true) {
//std::cout << busyThreadNum_ << " vs " << livThreadNum_ << std::endl;
if(taskSize_ > livThreadNum_ / 2) {
for(int i = 0; i < minThreadNum_ && livThreadNum_ < maxThreadNum_; i++) {
std::shared_ptr<Thread> thread(new Thread(&cond_, &mutex_, std::shared_ptr<ThreadPool>(this)));
thread->Start();
threadVector_.push_back(thread);
livThreadNum_++;
}
} else {
for(int i = 0; i < livThreadNum_ - taskSize_ * 2 && livThreadNum_ > minThreadNum_; i++) {
if(threadVector_.at(0)->IsAlive()) {
threadVector_.at(0)->Shut();
livThreadNum_--;
threadVector_.pop_front();
} else {
threadVector_.pop_front();
livThreadNum_--;
}
}
}
}
}