aboutsummaryrefslogtreecommitdiff
path: root/works/life/operating-system-experiment/Thread.h
diff options
context:
space:
mode:
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 c71c11f..79c1728 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>
@@ -38,6 +46,8 @@ private:
#ifdef CRU_WINDOWS
DWORD thread_id_ = 0;
HANDLE thread_handle_ = nullptr;
+#else
+ std::unique_ptr<pthread_t> thread_;
#endif
};
@@ -45,6 +55,7 @@ namespace details {
#ifdef CRU_WINDOWS
CRU_API DWORD WINAPI ThreadProc(_In_ LPVOID lpParameter);
#else
+void *ThreadProc(void *data);
#endif
} // namespace details
@@ -59,6 +70,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