Files
NP_Midterm/include/Utils/Thread.hpp

102 lines
3.0 KiB
C++

#pragma once
#ifdef _WIN32
#include <windows.h>
#include <process.h>
#elif __linux__
#include <pthread.h>
#else
#error "이 플랫폼은 지원되지 않습니다."
#endif
#include <functional>
#include <future>
#include <typeinfo>
#include "Socket/Log.hpp"
namespace Chattr {
class Thread {
public:
#ifdef _WIN32
static unsigned __stdcall thread_func(LPVOID param) {
auto task = static_cast<std::packaged_task<void()>*>(param);
(*task)();
delete task;
return 0;
}
#elif __linux__
static void* thread_func(void *param) {
auto task = static_cast<std::packaged_task<void()>*>(param);
(*task)();
delete task;
return 0;
}
#endif
Thread(Thread&&) noexcept;
Thread& operator=(Thread&&) noexcept;
Thread(const Thread&) = delete;
Thread& operator=(Thread&) = delete;
template<typename _Callable, typename... _Args>
requires (!std::is_same_v<std::decay_t<_Callable>, Thread>) &&
(!std::is_void_v<std::invoke_result_t<_Callable, _Args...>>)
Thread(_Callable&& __f, _Args&&... __args) {
auto boundFunc = [this, __f, ... __args = std::move(__args)]() mutable {
returnValuePtr = std::make_shared<std::invoke_result_t<_Callable, _Args...>>(__f(std::move(__args)...));
};
std::packaged_task<void()>* funcPtr = new std::packaged_task<void()>(std::move(boundFunc));
#ifdef _WIN32
handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, funcPtr, 0, nullptr);
#elif __linux__
pthread_create(&handle_, NULL, thread_func, funcPtr);
#endif
}
template<typename _Callable, typename... _Args>
requires (!std::is_same_v<std::decay_t<_Callable>, Thread>) &&
std::is_void_v<std::invoke_result_t<_Callable, _Args...>>
Thread(_Callable&& __f, _Args&&... __args) {
auto boundFunc = [this, __f, ... __args = std::move(__args)]() mutable {
__f(std::move(__args)...);
};
std::packaged_task<void()>* funcPtr = new std::packaged_task<void()>(std::move(boundFunc));
#ifdef _WIN32
handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, funcPtr, 0, nullptr);
#elif __linux__
int rc = pthread_create(&handle_, NULL, thread_func, funcPtr);
#endif
if (handle_ <= 0 || rc != 0)
log::critical("pthread_create()");
}
~Thread();
void join() {
#ifdef _WIN32
WaitForSingleObject(handle_, INFINITE);
#elif __linux__
pthread_join(handle_, nullptr);
#endif
}
template<typename _RetType>
requires (!std::is_void_v<_RetType>)
_RetType join() {
#ifdef _WIN32
WaitForSingleObject(handle_, INFINITE);
#elif __linux__
pthread_join(handle_, nullptr);
#endif
return *static_cast<_RetType *>(returnValuePtr);
}
void detach();
private:
#ifdef _WIN32
HANDLE handle_;
#elif __linux__
pthread_t handle_;
#endif
std::shared_ptr<void> returnValuePtr;
bool detached = false;
};
} // namespace Chattr