-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.cpp
More file actions
48 lines (43 loc) · 1.31 KB
/
threadpool.cpp
File metadata and controls
48 lines (43 loc) · 1.31 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
#include "include/threadpool.h"
#include <unistd.h>
#include <iostream>
Practice::ThreadPool::ThreadPool(int initThreadsNum, int maxThreadsNum) {
if (initThreadsNum > maxThreadsNum) initThreadsNum = maxThreadsNum;
mMaxThreadsNum = maxThreadsNum;
// 线程池初始化时先启动ManageThread
if (mManageThread == NULL) {
mManageThread = new ManageThread(&mPool, mMaxThreadsNum);
}
mManageThread->start();
// 添加WorkThread
for (int i = 0; i < initThreadsNum; i++) {
WorkThread *t = new WorkThread(mManageThread);
t->start();
mPool.push_back(t);
}
}
Practice::ThreadPool::~ThreadPool() {
// 停止管理线程
mManageThread->cancel();
// 停止工作线程
for (std::list<WorkThread *>::iterator iter = mPool.begin();
iter != mPool.end(); iter++) {
if ((*iter)->isBusy()) {
// 线程还有任务未执行完毕则发出信号等待执行完毕
(*iter)->notifyFinish();
} else {
(*iter)->cancel();
}
}
sleep(5);
// 释放内存
delete mManageThread;
std::cout << "ManageThread free" << '\n';
for (std::list<WorkThread *>::iterator iter = mPool.begin();
iter != mPool.end(); iter++) {
delete *iter;
// mPool.erase(iter); 有问题
}
mPool.clear();
}
void Practice::ThreadPool::addTask(Task *task) { mManageThread->addTask(task); }