-
Notifications
You must be signed in to change notification settings - Fork 10
/
ThreadPool.h
42 lines (32 loc) · 982 Bytes
/
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
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#include "NonCopyable.h"
#include "MutexLock.h"
#include "Condition.h"
#include <queue>
#include <functional>
#include <memory>
class Thread;
class ThreadPool : private NonCopyable
{
public:
typedef std::function<void()> Task;
ThreadPool(size_t queueSize, size_t poolSize);
~ThreadPool();
void start(); //启动线程池
void stop(); //停止线程池
void addTask(const Task &);
private:
Task getTask();
void runInThread(); //线程池内线程的回调函数
mutable MutexLock mutex_;
Condition empty_;
Condition full_;
size_t queueSize_; //队列大小
std::queue<Task> queue_;
size_t poolSize_; //线程池的大小
//PtrVector<Thread> threads_;
std::vector<std::unique_ptr<Thread> > threads_;
bool isStarted_; //线程池是否开启
};
#endif /*THREAD_POOL_H*/