-
Notifications
You must be signed in to change notification settings - Fork 1
/
TaskQueueThread.cpp
215 lines (189 loc) · 3.75 KB
/
TaskQueueThread.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "StdAfx.h"
#include ".\taskqueuethread.h"
#include <process.h>
#include <tchar.h>
int CTaskQueueThread::m_nID = 0;
CTaskQueueThread::CTaskQueueThread(LPCSTR szThreadName/* = nullptr*/)
:m_bSingleTask(false), m_hThread(NULL), m_bExit(false)
{
CString strEventName = _T("CTaskQueueThread_Event_");
strEventName.Append(CI2W(GetCurrentProcessId()));
strEventName.Append(_T("_"));
strEventName.Append(CI2W(m_nID++));
m_hEvent = CreateEvent(NULL, TRUE, TRUE, strEventName);
if (szThreadName != nullptr)
{
m_strThreadName = szThreadName;
}
}
CTaskQueueThread::CTaskQueueThread(FNCallBack *pFunc, void* param
, bool bSingleTask/* = true*/, LPCSTR szThreadName/* = nullptr*/):m_bExit(false)
,m_bSingleTask(bSingleTask),m_hThread(NULL)
{
PushTask(pFunc, param);
CString strEventName = _T("CTaskQueueThread_Event_");
strEventName.Append(CI2W(GetCurrentProcessId()));
strEventName.Append(_T("_"));
strEventName.Append(CI2W(m_nID++));
m_hEvent = CreateEvent(NULL, TRUE, TRUE, strEventName);
SetEvent(m_hEvent);
if (szThreadName != nullptr)
{
m_strThreadName = szThreadName;
}
}
CTaskQueueThread::~CTaskQueueThread(void)
{
if (!m_bSingleTask)
{
m_bExit = true;
::SetEvent(m_hEvent);
}
if (m_hThread != nullptr)
{
CloseHandle(m_hThread);
m_hThread = nullptr;
}
}
BOOL CTaskQueueThread::PushTask(FNCallBack *pFunc, void* param)
{
stTask task;
task.pFunc = pFunc;
task.param = param;
CAutoLock _lock(&m_csQueueFnCallBack);
m_queueFnCallBack.push(task);
if (!m_bSingleTask)
{
::SetEvent(m_hEvent);
}
return m_queueFnCallBack.size()==1;
}
bool CTaskQueueThread::ClearTask(bool bExit/* = true*/)
{
LD("enter");
CAutoLock _lock(&m_csQueueFnCallBack);
while(!m_queueFnCallBack.empty())
{
m_queueFnCallBack.pop();
}
LD("exit "<<m_queueFnCallBack.size());
m_bExit = bExit;
::SetEvent(m_hEvent);
return true;
}
BOOL CTaskQueueThread::Start()
{
if(isRunning())
{
return FALSE;
}
if(NULL != m_hThread)
{
CloseHandle(m_hThread);
m_hThread = NULL;
}
unsigned int nThreadID;
m_hThread = (HANDLE)_beginthreadex(NULL, 0, Run, this, 1, &nThreadID);
if (!m_strThreadName.empty())
{
SetThreadName(nThreadID, m_strThreadName.c_str());
}
return TRUE;
}
void CTaskQueueThread::Terminate()
{
if(NULL == m_hThread)
{
return;
}
::TerminateThread(m_hThread, 0);
CloseHandle(m_hThread);
m_hThread = NULL;
}
HANDLE CTaskQueueThread::GetThreadHandle()
{
return m_hThread;
}
void CTaskQueueThread::ExecuteTasks()
{
bool bEmpty = false;
{
CAutoLock _lock(&m_csQueueFnCallBack);
bEmpty = m_queueFnCallBack.empty();
}
while (!bEmpty)
{
if (m_bExit)
{
break;
}
stTask task;
{
CAutoLock _lock(&m_csQueueFnCallBack);
task = m_queueFnCallBack.front();
m_queueFnCallBack.pop();
bEmpty = m_queueFnCallBack.empty();
}
(*(task.pFunc))(task.param);
}
}
unsigned int __stdcall CTaskQueueThread::Run(void *param)
{
CTaskQueueThread *pThis = (CTaskQueueThread *)param;
if (pThis == NULL)
{
return 0;
}
if (pThis->m_bSingleTask)
{
pThis->ExecuteTasks();
}
else
{
while(true)
{
DWORD dwRet = WaitForSingleObject(pThis->m_hEvent, INFINITE);
if (WAIT_FAILED == dwRet)
{
break;
}
if (pThis->m_bExit)
{
break;
}
ResetEvent(pThis->m_hEvent);
pThis->ExecuteTasks();
}
//m_hEvent¿ÉÄÜÔÚ±ðµÄÏß³ÌÊÍ·ÅÁË
//CloseHandle(pThis->m_hEvent);
//pThis->m_hEvent = nullptr;
}
return 0;
}
bool CTaskQueueThread::isRunning()
{
if(NULL == m_hThread)
{
return false;
}
DWORD ec = 0;
return GetExitCodeThread(m_hThread, &ec) && ec == STILL_ACTIVE;
}
void CTaskQueueThread::Wait()
{
if(NULL == m_hThread)
{
return;
}
switch (WaitForSingleObject(m_hThread, INFINITE))
{
case WAIT_OBJECT_0:
{
CloseHandle(m_hThread);
m_hThread = NULL;
return;
}
default:
break;
}
}