-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.h
More file actions
64 lines (46 loc) · 1.43 KB
/
Copy paththreadpool.h
File metadata and controls
64 lines (46 loc) · 1.43 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
#pragma once
#include <pthread.h>
#include <unistd.h>
#include <cstring>
#include <iostream>
#include "taskQueue.h"
namespace louis {
namespace thread {
// 线程池类
template <class T>
class ThreadPool {
public:
// 创建线程池
ThreadPool(int min, int max);
// 销毁线程池
~ThreadPool();
// 添加任务到线程池
bool addTask(callback process, void* arg);
// 获取线程池状态
int getBusyCount();
int getAliveCount();
private:
// 管理者线程函数
static void* manager(void* arg);
// 工作线程函数
static void* worker(void* arg);
// 单个线程退出
void threadExit();
private:
// 任务队列
TaskQueue<T>* TaskQueue_;
pthread_t* workerThreadIDs_; // 工作线程数组
pthread_t managerThreadID_; // 管理者线程
int minThreads_; // 最小线程数
int maxThreads_; // 最大线程数
int busyCount_; // 忙碌线程计数
int liveCount_; // 存活线程计数
int exitCount_; // 退出线程计数
bool shutdown_ = false; // 关闭标志(类内初始化C++11)
pthread_mutex_t poolMutex_; // 保护线程池的互斥锁
pthread_cond_t notEmpty_; // 队列非空条件
static const int COUNT = 2; // 每次增加或减少的线程数量
};
} // namespace Thread
} // namespace Louis
#include "threadpool.inl"