-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththreadpool.h
47 lines (36 loc) · 828 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
43
44
45
46
47
#pragma once
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <vector>
#include <memory>
#include <queue>
#include <functional>
#include <pthread.h>
#include "mutex.h"
#include "thread.h"
#include "condition.h"
class ThreadPool
{
public:
typedef std::function<void ()> CallFunc;
explicit ThreadPool(int taskNum);
~ThreadPool();
ThreadPool(const ThreadPool &) = delete;
ThreadPool & operator= (const ThreadPool &) = delete;
void Start(int threadNum);
void Run(CallFunc func);
// FIX ME: the rest tasks in the m_tasks will not be done, if we stop
void Stop();
private:
CallFunc Take();
void RunInThread();
private:
bool m_bRunning;
int m_maxTaskNum;
Mutex m_mutex;
Condition m_notFull;
Condition m_notEmpty;
std::vector<std::unique_ptr<Thread>> m_threads;
std::deque<CallFunc> m_tasks;
};
#endif