#pragma once #ifdef _WIN32 #include #include #elif __linux__ #include #else #error "이 플랫폼은 지원되지 않습니다." #endif #include #include namespace Chattr { class Thread { public: #ifdef _WIN32 static unsigned __stdcall thread_func(LPVOID param) { auto task = static_cast*>(param); (*task)(); delete task; return 0; } #elif __linux__ static void* thread_func(void *param) { auto task = static_cast*>(param); (*task)(); delete task; return 0; } #endif Thread(Thread&&) noexcept; Thread& operator=(Thread&&) noexcept; Thread(const Thread&) = delete; Thread& operator=(Thread&) = delete; template requires (!std::is_same_v, Thread>) Thread(_Callable&& __f, _Args&&... __args) { auto boundFunc = [this, __f = std::move(__f), ... __args = std::move(__args)]() mutable { if constexpr (!std::is_void_v>) { returnValue = __f(std::move(__args)...); } else { __f(std::move(__args)...); // void 반환은 저장하지 않음 } }; std::function funcPtr = std::move(boundFunc); #ifdef _WIN32 handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, new std::function(std::move(funcPtr)), 0, nullptr); #elif __linux__ pthread_create(&handle_, NULL, thread_func, funcPtr); #endif } ~Thread(); void* join(); void detach(); private: #ifdef _WIN32 HANDLE handle_; #elif __linux__ pthread_t handle_; #endif void* returnValue = nullptr; bool detached = false; }; } // namespace Chattr