aboutsummaryrefslogtreecommitdiff
path: root/works/life/operating-system-experiment/Thread.h
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-06-09 22:31:11 +0800
committercrupest <crupest@outlook.com>2021-06-09 22:31:11 +0800
commit59be99aad9d7ad36e3edf648dbbd6b5b215c125c (patch)
tree73cf88107c915ed1657d6e2a6b9584cc48c52113 /works/life/operating-system-experiment/Thread.h
parent8268724a054a42f9446736c42d8f04a0207aa12d (diff)
parent8bdb5db67fa3eeda755511f91fd257bde548a18f (diff)
downloadcrupest-59be99aad9d7ad36e3edf648dbbd6b5b215c125c.tar.gz
crupest-59be99aad9d7ad36e3edf648dbbd6b5b215c125c.tar.bz2
crupest-59be99aad9d7ad36e3edf648dbbd6b5b215c125c.zip
import(life): Merge branch 'main' of https://github.com/crupest/life
Diffstat (limited to 'works/life/operating-system-experiment/Thread.h')
-rw-r--r--works/life/operating-system-experiment/Thread.h17
1 files changed, 17 insertions, 0 deletions
diff --git a/works/life/operating-system-experiment/Thread.h b/works/life/operating-system-experiment/Thread.h
index cdeecac..69a402c 100644
--- a/works/life/operating-system-experiment/Thread.h
+++ b/works/life/operating-system-experiment/Thread.h
@@ -3,9 +3,17 @@
#include "Base.h"
+#ifdef CRU_WINDOWS
+#include <Windows.h>
+#else
+#include <pthread.h>
+#endif
+
#ifdef __cplusplus
+#include <cassert>
#include <functional>
+#include <memory>
#include <tuple>
#include <type_traits>
#include <utility>
@@ -39,6 +47,8 @@ private:
#ifdef CRU_WINDOWS
DWORD thread_id_ = 0;
HANDLE thread_handle_ = nullptr;
+#else
+ std::unique_ptr<pthread_t> thread_;
#endif
};
@@ -46,6 +56,7 @@ namespace details {
#ifdef CRU_WINDOWS
CRU_API DWORD WINAPI ThreadProc(_In_ LPVOID lpParameter);
#else
+void *ThreadProc(void *data);
#endif
} // namespace details
@@ -60,6 +71,12 @@ Thread::Thread(Fn &&process, Args &&...args) {
#ifdef CRU_WINDOWS
thread_handle_ = ::CreateThread(nullptr, 0, &::cru::details::ThreadProc,
static_cast<void *>(p), 0, &thread_id_);
+ assert(thread_handle_);
+#else
+ thread_.reset(new pthread_t());
+ auto c = pthread_create(thread_.get(), nullptr, details::ThreadProc,
+ static_cast<void *>(p));
+ assert(c == 0);
#endif
};
} // namespace cru