From 69f7d1bd745ed5680b9bc4e3cfdd882ff2a5ad26 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 27 Mar 2019 22:18:14 +0800 Subject: +threading --- .../threading/thread_impl_win32.cpp | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'source/libs/asura-lib-utils/threading/thread_impl_win32.cpp') diff --git a/source/libs/asura-lib-utils/threading/thread_impl_win32.cpp b/source/libs/asura-lib-utils/threading/thread_impl_win32.cpp index e69de29..fd1b066 100644 --- a/source/libs/asura-lib-utils/threading/thread_impl_win32.cpp +++ b/source/libs/asura-lib-utils/threading/thread_impl_win32.cpp @@ -0,0 +1,69 @@ +#include "thread_impl_win32.h" +#include "thread.h" + +namespace AsuraEngine +{ + namespace Threading + { + + static DWORD WINAPI _thread_win32_runner(LPVOID param) + { + Thread* thread = (Thread*)param; + thread->Execute(); + return 0; + } + + ThreadImplWin32::ThreadImplWin32() + { + } + + ThreadImplWin32::~ThreadImplWin32() + { + if (!mHandle) return; + ::CloseHandle(mHandle); + mHandle = 0; + } + + bool ThreadImplWin32::Start(Thread* thread, uint32 stacksize/*=0*/) + { + assert(!IsRunning()); + mHandle = ::CreateThread( + NULL + , stacksize + , _thread_win32_runner + , thread + , 0 /*创建后立即进行调度*/ + , NULL); + + return mHandle; + } + + void ThreadImplWin32::Join() + { + // 父线程等待此线程返回 + ::WaitForSingleObject(mHandle, INFINITE); + } + + void ThreadImplWin32::Kill() + { + ::TerminateThread(mHandle, FALSE); + } + + bool ThreadImplWin32::IsRunning() + { + if (mHandle) { + DWORD exitCode = 0; + // https://blog.csdn.net/yuanmeng567/article/details/19485719 + ::GetExitCodeThread(mHandle, &exitCode); + return exitCode == STILL_ACTIVE; + } + return false; + } + + bool ThreadImplWin32::IsCurrent() + { + return mHandle == ::GetCurrentThread(); + } + + } +} \ No newline at end of file -- cgit v1.1-26-g67d0