-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPool.h
More file actions
executable file
·86 lines (73 loc) · 1.24 KB
/
ThreadPool.h
File metadata and controls
executable file
·86 lines (73 loc) · 1.24 KB
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
#ifndef _THREAD_POOL_
#define _THREAD_POOL_
#include <boost/thread.hpp>
#include <boost/noncopyable.hpp>
#include <boost/function.hpp>
#include "TaskQueue.h"
#include "stdio.h"
//typedef boost::function<void(void)> FUNCTION_DEF;
template<typename FUNCTION_DEF>
class ThreadPool:boost::noncopyable
{
public:
/* enum ECallType
{
FUNCTION_CALL = 0,
CLASS_CALL
};*/
ThreadPool(int num)
:m_ThreadNum(num),m_IsRun(false)
{
}
~ThreadPool()
{
}
void Init()
{
m_IsRun = true;
// m_eCallType = calltype;
if(m_ThreadNum <= 0)
return;
for(int i=0;i < m_ThreadNum;++i)
{
m_ThreadGroup.add_thread(new boost::thread(boost::bind(&ThreadPool::run,this)));
}
}
void Stop()
{
m_IsRun = false;
}
void Post(const FUNCTION_DEF& task)
{
m_TaskQueue.PushTask(task);
}
void Wait()
{
m_TaskQueue.stop();
m_ThreadGroup.join_all();
}
private:
TaskQueue<FUNCTION_DEF> m_TaskQueue;
boost::thread_group m_ThreadGroup;
int m_ThreadNum;
volatile bool m_IsRun;
// ECallType m_eCallType;
// void run();
void run()
{
while(m_IsRun)
{
FUNCTION_DEF task=m_TaskQueue.TakeTask();
if(task != NULL)
{
//printf("task != NULL\n");
task();
}
/*else
{
printf("task == NULL\n");
}*/
}
}
};
#endif