blob: 0bede986265934f2ebe73f29caf8aae17a613dd7 (
plain)
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
|
#include <string>
#include "Thread.h"
#include "../Utilities/Assert.h"
#include "../Utilities/Type.h"
static std::string s_ThreadErr;
static DWORD WINAPI ThreadMain(LPVOID param)
{
Thread* thread = (Thread*)param;
thread->Run();
return NULL;
}
Thread::Thread(uint32 stacksize)
{
m_Handle = ::CreateThread(
NULL
, stacksize
, ThreadMain
, this
, CREATE_SUSPENDED
, NULL);
if (m_Handle == 0)
{
s_ThreadErr = "Create Thread Failed. ErrorCode=" + std::to_string(GetLastError());
throw ThreadException(s_ThreadErr.c_str());
}
}
Thread::~Thread()
{
CloseHandle(m_Handle);
}
void Thread::Resume()
{
::ResumeThread(m_Handle);
}
bool Thread::IsRunning()
{
if (m_Handle) {
DWORD exitCode = 0;
// https://blog.csdn.net/yuanmeng567/article/details/19485719
::GetExitCodeThread(m_Handle, &exitCode);
return exitCode == STILL_ACTIVE;
}
return false;
}
void Thread::Join()
{
::WaitForSingleObject(m_Handle, INFINITE);
}
void Thread::Kill()
{
::TerminateThread(m_Handle, FALSE);
}
void Thread::Sleep(uint ms)
{
::Sleep(ms);
}
bool Thread::IsCurrent()
{
return m_Handle == ::GetCurrentThread();
}
|