-
Notifications
You must be signed in to change notification settings - Fork 17
/
thread_pool.h
50 lines (42 loc) · 1.52 KB
/
thread_pool.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
/*======================================================
> File Name: thread_pool.h
> Author: MiaoShuai
> E-mail:
> Other :
> Created Time: 2015年12月27日 星期日 12时44分42秒
=======================================================*/
#ifndef THREAD_POOL_H_
#define THREAD_POOL_H_
#include <thread>
#include <mutex>
#include <condition_variable>
#include <list>
#include <vector>
#include <memory>
#include <functional>
namespace netlib
{
class ThreadPool
{
public:
typedef std::function<void(void)> Task;
ThreadPool(int threadNumber);
~ThreadPool();
//往任务队列里添加任务
bool append(Task task);
//启动线程池
bool start(void);
//停止线程池
bool stop(void);
private:
//线程所执行的工作函数
void threadWork(void);
std::mutex mutex_; //互斥锁
std::condition_variable_any condition_empty_; //当任务队列为空时的条件变量
std::list<Task> tasks_; //任务队列
bool running_; //线程池是否在运行
int threadNumber_; //线程数
std::vector<std::shared_ptr<std::thread>> threads_; //用来保存线程对象指针
};
}
#endif