diff options
author | crupest <crupest@outlook.com> | 2021-06-09 22:28:01 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-06-09 22:28:01 +0800 |
commit | 85481d521cfb996e7487a95f3727474d79e27281 (patch) | |
tree | 967f655f14657cf0392fc567fb8144f0101b79a1 /operating-system-experiment/Thread.h | |
parent | a2cf634d1bceb3614b1f5adcab5b2b41cddbd3c4 (diff) | |
download | life-85481d521cfb996e7487a95f3727474d79e27281.tar.gz life-85481d521cfb996e7487a95f3727474d79e27281.tar.bz2 life-85481d521cfb996e7487a95f3727474d79e27281.zip |
...
Diffstat (limited to 'operating-system-experiment/Thread.h')
-rw-r--r-- | operating-system-experiment/Thread.h | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/operating-system-experiment/Thread.h b/operating-system-experiment/Thread.h index c71c11f..79c1728 100644 --- a/operating-system-experiment/Thread.h +++ b/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
|