-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadPool.h
More file actions
66 lines (47 loc) · 1.24 KB
/
Copy paththreadPool.h
File metadata and controls
66 lines (47 loc) · 1.24 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
#ifndef TASKQUEUE_H
#define TASKQUEUE_H
#include "TaskQueue.h"
template <typename T>
class ThreadPool
{
private:
//任务队列
TaskQueue<T>* taskQ;
//管理者线程
pthread_t managerID; //管理者线程ID
//工作者线程
pthread_t *threadIDs; //工作的线程ID
//线程数: 指定线程数的范围
int minNum;
int maxNum;
//当前工作的线程数
int busyNum;
//当前存活的线程数,已创建的线程个数
int liveNum;
//空闲的线程,方便在任务较少时杀死这些线程
int exitNum;
//防止线程访问冲突,需要加锁
pthread_mutex_t mutexPool; //线程池的锁
pthread_cond_t notEmpty; //判断是否空
//每次增加的线程个数
static const int NUMBER=2;
//销毁线程
bool shutDown; //销毁线程池为1,不销毁为0
public:
//创建线程池并初始化: 最大和最小线程数以及任务队列的容量
ThreadPool(int min,int max);
//销毁线程池
~ThreadPool();
//向线程池中添加任务
void addTask(Task<T> task);
//当前线程池中线程个数
int getBusyNum();
//获取当前创建的线程个数
int getAliveNum();
private:
//工作函数
static void* worker(void* arg);
static void* manager(void* arg);
void threadExit();
};
#endif