-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNA_Thread.cpp
58 lines (44 loc) · 1.13 KB
/
NA_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
#include "NA_Thread.h"
#include "globals.h"
#include <iostream>
using std::cerr;
void NA_Thread::startThread()
{
threadHandle = CreateThread(NULL, 0, threadProc, this, 0, threadID);
}
NA_Thread::NA_Thread()
{
}
NA_Thread::~NA_Thread()
{
requestSelfTerminate();
if (threadHandle)
{
WaitForSingleObject(threadHandle, INFINITE);
CloseHandle(threadHandle);
}
}
void NA_Thread::requestSelfTerminate()
{
terminateRequested = true;
}
LPDWORD NA_Thread::getExitCode()
{
LPDWORD exitCode;
if (GetExitCodeThread(threadHandle, exitCode) != 0)
{
NA_consoleOutputCritSect.enter();
cerr << "NA_Thread::Destructor - failed to get thread exit code. Error: " << GetLastError() << "\n";
NA_consoleOutputCritSect.leave();
}
return exitCode;
}
bool NA_Thread::waitForTerminate(DWORD milliseconds)
{
DWORD w = WaitForSingleObject(threadHandle, milliseconds);
return !(w == WAIT_TIMEOUT); //LOW: Should check for errors https://msdn.microsoft.com/en-us/library/windows/desktop/ms687032(v=vs.85).aspx
}
bool NA_Thread::isActive()
{
return (DWORD)getExitCode() == STILL_ACTIVE;
}