forked from Chuyu-Team/CPPHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thread.h
240 lines (188 loc) · 3.66 KB
/
Thread.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#pragma once
#include <Windows.h>
#include <xutility>
#include "CriticalSectionHelper.h"
#include <atlutil.h>
///////////////////////////////////////////////////////////////////////////////////////////////
//
// 自制的一个轻量级线程封装类
//
//////////////////////////////////////////////////////////////////////////////////////////////
class Thread
{
public:
HANDLE hThread;
template<class _Ptr>
//dwCreationFlags请参考CreateThread
Thread(_Ptr& _ptr, DWORD dwCreationFlags = 0)
{
hThread = (HANDLE)_beginthreadex(NULL, 0, [](void * UserData) ->unsigned
{
auto Ptr = (_Ptr*)UserData;
(*Ptr)();
delete Ptr;
return 0;
}, (void*)new _Ptr(std::move(_ptr)), dwCreationFlags, NULL);
}
//抑制复制构造等函数
Thread(Thread const&);
Thread& operator=(Thread const&);
Thread()
:hThread(NULL)
{
}
Thread(Thread &&Item)
:hThread(Item.Detach())
{
}
~Thread()
{
if (hThread)
{
CloseHandle(hThread);
}
}
HANDLE Detach()
{
HANDLE TempThread = hThread;
hThread = NULL;
return TempThread;
}
//恢复线程运行
DWORD ResumeThread()
{
return ::ResumeThread(hThread);
}
//挂起线程
DWORD SuspendThread()
{
return ::SuspendThread(hThread);
}
//等待线程,dwMilliseconds等同于WaitForSingleObject
DWORD WaitThread(DWORD dwMilliseconds = INFINITE)
{
return WaitForSingleObject(hThread, dwMilliseconds);
}
};
#define Task Thread
class CATLThreadPoolWorker
{
public:
std::function<void()> Call;
HANDLE hWorkEvent;
volatile LONG Count;
BOOL bCancelled;
template<class _Ptr>
//dwCreationFlags请参考CreateThread
CATLThreadPoolWorker(_Ptr&& _ptr)
: Call(std::move(_ptr))
, Count(0)
, bCancelled(FALSE)
, hWorkEvent(CreateEvent(NULL, TRUE, TRUE, NULL))
{
}
//开始执行任务
void John()
{
if(!bCancelled)
Call();
Release();
}
//添加任务引用
void AddRef()
{
if (InterlockedIncrement(&Count)==1)
{
ResetEvent(hWorkEvent);
}
}
//减少任务引用
void Release()
{
if (InterlockedDecrement(&Count) == 0)
{
SetEvent(hWorkEvent);
}
}
DWORD __fastcall Wait(_In_ DWORD dwMilliseconds= INFINITE)
{
return WaitForSingleObject(hWorkEvent,dwMilliseconds);
}
};
class CATLThreadPoolWorkerInternal
{
public:
typedef CATLThreadPoolWorker* RequestType;
static BOOL Initialize(void *pvParam)
{
return TRUE;
}
static void Terminate(void* pvParam)
{
}
static void Execute(
_In_ typename RequestType request,
_In_ void *pvWorkerParam,
_In_ OVERLAPPED *pOverlapped
)
{
ATLASSERT(request != NULL);
request->John();
}
static BOOL GetWorkerData(DWORD /*dwParam*/, void ** /*ppvData*/)
{
return FALSE;
}
};
class CThreadPool2: public CThreadPool<CATLThreadPoolWorkerInternal>
{
public:
BOOL QueueRequest(_In_ typename CATLThreadPoolWorkerInternal::RequestType request)
{
request->AddRef();
if (CThreadPool<CATLThreadPoolWorkerInternal>::QueueRequest(request))
{
return TRUE;
}
else
{
//提交任务失败
request->Release();
return FALSE;
}
}
//并行For,使用指针
template<class Item, class _Ptr>
void For(Item* pItems, LONG Count, _Ptr& _ptr)
{
volatile LONG Index = -1;
CATLThreadPoolWorker Work([&Index, pItems,&_ptr]()
{
_ptr(pItems[InterlockedIncrement(&Index)]);
});
for (long i = 0; i != Count; ++i)
{
QueueRequest(&Work);
}
Work.Wait();
}
//并行For,使用迭代器
template<class iterator, class _Ptr>
void For(iterator Begin, iterator End, _Ptr& _ptr)
{
CCriticalSection Lock;
iterator pItems = Begin;
CATLThreadPoolWorker Work([&pItems,&Lock, &_ptr]()
{
Lock.Lock();
auto& Item = *(pItems++);
Lock.Unlock();
_ptr(Item);
});
for (; Begin!= End; ++Begin)
{
QueueRequest(&Work);
}
Work.Wait();
}
};