-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadpool.h
96 lines (83 loc) · 2.97 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
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <thread>
#include <mutex>
#include <queue>
#include <vector>
#include <map>
#include <condition_variable>
using std::queue;
using std::thread;
using std::mutex;
namespace Sloong
{
namespace Universal
{
/// C style define
typedef LPVOID(*pTaskJobFunc)(LPVOID);
typedef void(*pTaskCallBack)(long long, LPVOID);
typedef pTaskJobFunc LPTASKFUNC;
typedef pTaskCallBack LPTASKCALLBACK;
/// C++ std style define
typedef shared_ptr<void> SMARTER;
typedef SMARTER(*pSmartJobFunc)(SMARTER);
typedef void(*pSmartCallBack)(long long, SMARTER);
typedef pSmartJobFunc LPSMARTFUNC;
typedef pSmartCallBack LPSMARTCALLBACK;
typedef std::function<SMARTER(SMARTER)> SmartFunction;
typedef std::function<SMARTER(long long,SMARTER)> SmartCallbackFunction;
enum TaskType {
Normal,
SmartParam,
SmartFunc,
};
struct TaskParam
{
TaskType emTaskType;
ULONG nTaskID;
// Only C Style
LPTASKFUNC pJob = nullptr;
LPTASKCALLBACK pCallBack = nullptr;
LPVOID pParam = nullptr;
// Only C++ style
LPSMARTFUNC pSmartJob;
LPSMARTCALLBACK pSmartCallBack;
SMARTER pSmartParam;
// std::function style
SmartFunction pSmartFuncJob;
SmartCallbackFunction pSmartFuncCallback;
};
class UNIVERSAL_API CThreadPool
{
public:
static void Initialize(int nThreadNum);
static void Run();
static void Exit();
// Add a task to job list.
// the pJob is the job function pointer.
// the pParam is the job function param when call the function.
// the bStatic is the job is not doing once. if ture, the job will always run it in the threadpool.
// and the function return the job index in job list. for once job, it can not do anything, for static job
// it can used in RemoveTask function.
static ULONG EnqueTask(SmartFunction pJob, SMARTER pParam = nullptr, SmartCallbackFunction pCallBack = nullptr);
static ULONG EnqueTask(LPTASKFUNC pJob, LPVOID pParam = nullptr, LPTASKCALLBACK pCallBack = nullptr);
static ULONG EnqueTask(LPSMARTFUNC pJob, SMARTER pParam = nullptr, LPSMARTCALLBACK pCallBack = nullptr);
// Add a work thread to the threadlist.
// return the thread index in threadlist. if the nNum param is not 1, the other
// thread index is base on return value.
static int AddWorkThread(LPTASKFUNC pJob, LPVOID pParam = nullptr, int nNum = 1);
static int AddWorkThread(std::function<void(SMARTER)> pJob, SMARTER pParam = nullptr, int nNum = 1);
protected:
static map<ULONG, shared_ptr<TaskParam>> m_oJobList;
static vector<thread*> m_pThreadList;
static queue<ULONG> m_oWaitList;
static void ThreadWorkLoop(void);
static mutex g_oJobListMutex;
static mutex g_oRunJobMutex;
static condition_variable g_oRunJobCV;
static RUN_STATUS m_emStatus;
static ULONG m_nIDCursor;
};
}
}
#endif // !THREADPOOL_H