-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPool.cpp
More file actions
201 lines (189 loc) · 5.54 KB
/
Copy pathThreadPool.cpp
File metadata and controls
201 lines (189 loc) · 5.54 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
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "ThreadPool.h"
#include <unistd.h>
#include <pthread.h>
#include <iostream>
#include <string.h>
ThreadPool::ThreadPool(int minn, int maxx)
{
m_taskQ = new TaskQueue;
do
{
minnum = minn;
maxnum = maxx;
busynum = 0;
alivenum = minn;
workerIDs = new pthread_t[maxnum];
if (workerIDs == nullptr)
{
cout << "malloc thread_t[] 失败...." << endl;
;
break;
}
memset(workerIDs, 0, sizeof(pthread_t) * maxnum);
if (pthread_cond_init(¬Empty, nullptr) != 0 ||
pthread_mutex_init(&poolmutex, nullptr) != 0)
{
cout << "init lock failed..." << endl;
break;
}
for (int i = 0; i < minnum; i++)
{
pthread_create(&workerIDs[i], nullptr, worker, this);
cout << "创建子线程, ID: " << to_string(workerIDs[i]) << endl;
if (pthread_detach(workerIDs[i]))
{
delete[] workerIDs;
throw std::exception();
}
}
pthread_create(&managerID, nullptr, manager, this);
cout << "管理者线程已经创建..." << endl;
} while (0);
}
ThreadPool::~ThreadPool()
{
shutdown = true;
pthread_join(managerID, nullptr);
for (int i = 0; i < alivenum; i++)
{
pthread_cond_signal(¬Empty);
}
if (workerIDs)
delete[] workerIDs;
if (m_taskQ)
delete m_taskQ;
pthread_cond_destroy(¬Empty);
pthread_mutex_destroy(&poolmutex);
}
void ThreadPool::addtask(Task t)
{
if (shutdown)
return;
m_taskQ->addTask(t);
pthread_cond_signal(¬Empty);
}
int ThreadPool::getbusynum()
{
int bn = 0;
pthread_mutex_lock(&poolmutex);
bn = busynum;
pthread_mutex_unlock(&poolmutex);
return bn;
}
int ThreadPool::getalivenum()
{
int an = 0;
pthread_mutex_lock(&poolmutex);
an = alivenum;
pthread_mutex_unlock(&poolmutex);
return an;
}
void *ThreadPool::worker(void *arg)
{
ThreadPool *pool = static_cast<ThreadPool *>(arg);
while (1)
{
pthread_mutex_lock(&pool->poolmutex);
while (pool->m_taskQ->taskNumber() == 0 && !pool->shutdown)
{
cout << "thread " << to_string(pthread_self()) << " waiting..." << endl;
// 阻塞线程
pthread_cond_wait(&pool->notEmpty, &pool->poolmutex);
if (pool->exitnum > 0)
{
pool->exitnum--;
if (pool->alivenum > pool->minnum)
{
pool->alivenum--;
pthread_mutex_unlock(&pool->poolmutex);
pool->threadexit();
}
}
}
if (pool->shutdown)
{
pthread_mutex_unlock(&pool->poolmutex);
pool->threadexit();
}
Task t = pool->m_taskQ->takeTask();
pool->busynum++;
pthread_mutex_unlock(&pool->poolmutex);
cout << "thread " << to_string(pthread_self()) << " start working..." << endl;
t.function(t.arg);
delete t.arg;
t.arg = nullptr;
cout << "thread " << to_string(pthread_self()) << " end working...";
pthread_mutex_lock(&pool->poolmutex);
pool->busynum--;
pthread_mutex_unlock(&pool->poolmutex);
}
return nullptr;
}
void *ThreadPool::manager(void *arg)
{
ThreadPool *pool = static_cast<ThreadPool *>(arg);
while (!pool->shutdown)
{
sleep(3);
cout << "manager start work!" << endl;
pthread_mutex_lock(&pool->poolmutex);
int quesz = pool->m_taskQ->taskNumber();
int busyn = pool->busynum;
int aliven = pool->alivenum;
pthread_mutex_unlock(&pool->poolmutex);
// 创建
const int NUMBER = 2;
// aliven<quesz && aliven<maxNum
if (aliven < quesz && aliven < pool->maxnum)
{
cout << "add thread!" << endl;
pthread_mutex_lock(&pool->poolmutex);
int counter = 0;
for (int i = 0; i < pool->maxnum && pool->alivenum < pool->maxnum && counter < NUMBER; i++)
{
if (pool->workerIDs[i] == 0)
{
pthread_create(&pool->workerIDs[i], nullptr, worker, pool);
if (pthread_detach(pool->workerIDs[i]))
{
delete[] pool->workerIDs;
throw std::exception();
}
counter++;
pool->alivenum++;
cout << "添加线程:" << to_string(pthread_self()) << " 现在存活线程数:" << pool->alivenum << endl;
}
}
pthread_mutex_unlock(&pool->poolmutex);
}
// 销毁
//
if (busyn * 2 < aliven && aliven > pool->minnum)
{
cout << "destory thread!" << endl;
pthread_mutex_lock(&pool->poolmutex);
pool->exitnum = NUMBER;
pthread_mutex_unlock(&pool->poolmutex);
for (int i = 0; i < NUMBER; i++)
{
pthread_cond_signal(&pool->notEmpty);
}
}
}
return nullptr;
}
void ThreadPool::threadexit()
{
pthread_t tid = pthread_self();
for (int i = 0; i < maxnum; i++)
{
if (workerIDs[i] == tid)
{
workerIDs[i] = 0;
cout << "threadExit() function: thread "
<< to_string(pthread_self()) << " exiting..." << endl;
break;
}
}
pthread_exit(nullptr);
}