-
Notifications
You must be signed in to change notification settings - Fork 8
/
threadpool.h
133 lines (116 loc) · 3.27 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
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
#ifndef _THREADPOOL_H_
#define _THREADPOOL_H_
#include <queue>
#include <fstream>
#include <pthread.h>
#include "taskqueue.h"
#include "time.h"
using namespace std;
const size_t MAX_THREAD_NUM = 100;
const size_t TIME_PERIOD_SEC = 1;
const size_t OVERLOAD_TASKS = 100;
const size_t MIN_THREAD_NUM = 5;
/*线程状态*/
enum Threadstate{
isbusy = 0,
isidle,
newcreated
};
/* 线程类:每一个线程记录线程的id和线程状态
* 设置线程池类为友元
*/
class Thread{
friend class ThreadPool;
Thread():m_id(0),prev(NULL),next(NULL),m_state(newcreated){}
pthread_t m_id;
Thread *prev;
Thread *next;
Threadstate m_state;
};
/*线程池类:创建线程池,初始化初始线程数量,可以对idle和busy的线程进行调度
* 使用两个链表来维护idle和busy的线程。
* 可以为该线程池创建管理者,负责添加和销毁线程,并可以指示线程退出
*
*/
class ThreadPool{
public:
static ThreadPool *create_instance();
~ThreadPool();
bool init();
bool create_thread(size_t number);
bool decrease_thread();
// bool decrease_to_none();
bool check_decrease();
bool create_manager();
void manage_increase(ofstream& os);
void manage_decrease(ofstream& os);
void display_status(ostream &os);
unsigned int get_idle_number();
unsigned int get_busy_number();
unsigned int get_total_number();
bool add_task(TaskBase *task);
bool add_task(TaskBase &task);
ThreadPool* set_max_number(size_t number){
num_max_t = number;
return this;
}
ThreadPool* set_manager_flag(bool flag){
manager_flag = flag;
return this;
}
ThreadPool* set_during_seconds(size_t sec){
period_of = sec;
return this;
}
ThreadPool* set_overload_tasks(size_t size){
overload_tasks = size;
return this;
}
void clear_manager_id(){
m_manager = 0;
}
bool get_manager_flag() const{
return manager_flag;
}
size_t get_during_seconds() const{
return period_of;
}
size_t get_queue_size(){
return task_queue.size();
}
time_t get_run_time(){
time_t timer;
return difftime(time(&timer),start_time);
}
private:
//把构造函数和复制构造函数设置为私有,禁止复制,并实现单例
ThreadPool();
ThreadPool(const ThreadPool&);
ThreadPool& operator=(const ThreadPool&);
void add_to_idle(Thread *);
void add_to_busy(Thread *);
void delete_thread(Thread *);
static ThreadPool* m_instance; //保存唯一实例的指针
static void* thread_run (void *arg); //线程运行函数
static void* manager_run(void *arg); //管理者运行函数
private:
pthread_t m_manager; // 管理者线程id
pthread_mutex_t count_lock; // 局部锁
pthread_mutex_t list_lock; // 列表锁
bool initialed;
bool manager_flag;
size_t period_of; //
size_t overload_tasks; //任务过载数量
time_t start_time; // 线程池启动时间
size_t num_total; // 总的线程数
size_t num_min_t; // 最小线程数
size_t num_max_t; // 最大线程数
size_t num_of_idle;//空闲线程数
size_t num_of_busy;//忙碌线程数
Thread* idle_head; // idle双向链表头
Thread* idle_end;
Thread* busy_head; // busy双向链表头
Thread* busy_end;
TaskQueue task_queue; // 任务队列
};
#endif