-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadPool.h
49 lines (42 loc) · 1.19 KB
/
ThreadPool.h
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
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <iostream>
#include <vector>
#include <queue>
#include <functional>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <chrono>
#include <unistd.h>
class ThreadPool {
public:
ThreadPool(int minNum, int maxNum);
~ThreadPool();
void addTask(std::function<void()> task);
int getBusyNumber();
int getAliveNumber();
void shutdown();
private:
struct Task {
std::function<void()> function;
};
using TaskQueue = std::queue<Task>; //using new_type = existing_type;
void addThread();
void worker();
void manager();
private:
std::vector<std::thread>* m_threadIDs; // 线程ID列表
TaskQueue* m_taskQ; // 任务队列
std::shared_mutex m_taskLock; // 任务队列的读写锁
std::mutex m_countLock; // 线程计数的互斥锁
std::condition_variable m_notEmpty; // 条件变量,用于线程等待任务
int m_minNum; // 最小线程数
int m_maxNum; // 最大线程数
int m_busyNum; // 忙线程数
int m_aliveNum; // 活跃线程数
int m_exitNum; // 需要退出的线程数
bool m_shutdown; // 是否关闭线程池
};
#endif