summaryrefslogtreecommitdiff
path: root/Client/Source/Threading/Thread.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Client/Source/Threading/Thread.cpp')
-rw-r--r--Client/Source/Threading/Thread.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/Client/Source/Threading/Thread.cpp b/Client/Source/Threading/Thread.cpp
new file mode 100644
index 0000000..0bede98
--- /dev/null
+++ b/Client/Source/Threading/Thread.cpp
@@ -0,0 +1,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();
+}