-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathThread.cpp
80 lines (60 loc) · 1.43 KB
/
Thread.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
#include <Windows.h>
#include "Thread.h"
#include "Utils.h"
Thread::Thread(void)
{
_threadHandle = NULL;
_threadId = 0;
}
Thread::~Thread(void)
{
if (_threadHandle != NULL) {
stop();
// CloseHandle(_threadHandle);
_threadHandle = NULL;
_threadId = 0;
}
}
bool Thread::start(size_t stackSize /* = 0 */)
{
if (!this->init())
return false;
_threadHandle = ::CreateThread(NULL, stackSize, threadProc, this, 0, &_threadId);
if (_threadHandle == NULL)
return false;
return true;
}
void Thread::stop(int exitCode)
{
::TerminateThread(_threadHandle, (DWORD )exitCode);
CloseHandle(_threadHandle);
_threadHandle = NULL;
_threadId = 0;
}
static DWORD exceptionFilter(EXCEPTION_POINTERS* excepInfo)
{
MyTrace("%s:%d - exception code: %d, exception address: %p",
__FILE__, __LINE__, excepInfo->ExceptionRecord->ExceptionCode,
excepInfo->ExceptionRecord->ExceptionAddress);
// assert(false);
return EXCEPTION_EXECUTE_HANDLER;
}
DWORD __stdcall Thread::threadProc(void* param)
{
// #define PAGE_SIZE 4096
// BYTE guardPages[PAGE_SIZE * 2 + 1];
// DWORD oldProt;
// VirtualProtect(&guardPages[PAGE_SIZE + 1], 1, PAGE_READONLY, &oldProt);
Thread* thisPtr = (Thread* )param;
DWORD result;
// __try {
result = (DWORD )thisPtr->run();
// } __except(exceptionFilter(GetExceptionInformation())) {
// }
thisPtr->final(result);
return result;
}
void Thread::wait()
{
WaitForSingleObject(_threadHandle, INFINITE);
}