리눅스 스레드 생성 & 핸들 저장장

This commit is contained in:
2025-04-24 20:40:24 +09:00
parent aaaf706feb
commit 85cc4ab0c5
3 changed files with 27 additions and 5 deletions

View File

@@ -2,7 +2,7 @@
#ifdef _WIN32
#include <windows.h>
#elif __linux__
#include <pthread.h>
#else
#error "이 플랫폼은 지원되지 않습니다."
#endif
@@ -10,18 +10,30 @@
namespace Chattr {
class Thread {
public:
#ifdef _WIN32
static DWORD WINAPI __thread(LPVOID param) {
std::unique_ptr<std::function<void()>> func(reinterpret_cast<std::function<void()>*>(param));
static DWORD WINAPI thread_func(LPVOID param) {
std::unique_ptr<std::function<void()>> func(static_cast<std::function<void()>*>(param));
(*func)();
return 0;
}
#elif __linux__
static void* thread_func(void *param) {
std::unique_ptr<std::function<void()>> func(static_cast<std::function<void()>*>(param));
(*func)();
return 0;
}
#endif
template<typename Callable, typename... Args>
Thread(Callable&& f, Args&&... args);
private:
#ifdef _WIN32
HANDLE handle_;
#elif __linux__
pthread_t handle_;
#endif
};
template<typename Callable, typename... Args>
@@ -31,7 +43,9 @@ Thread::Thread(Callable&& f, Args&&... args) {
auto funcPtr = new std::function<void()>(boundFunc);
#ifdef _WIN32
CreateThread(nullptr, 0, __thread, funcPtr, 0, nullptr);
handle_ = CreateThread(nullptr, 0, thread_func, funcPtr, 0, nullptr);
#elif __linux__
pthread_create(&handle_, NULL, thread_func, funcPtr);
#endif
}