Thread 고치기
This commit is contained in:
@@ -23,7 +23,7 @@ public:
|
||||
}
|
||||
#elif __linux__
|
||||
static void* thread_func(void *param) {
|
||||
auto task = static_cast<std::function<void* ()>*>(param);
|
||||
auto task = static_cast<std::function<void()>*>(param);
|
||||
(*task)();
|
||||
delete task;
|
||||
return 0;
|
||||
@@ -36,15 +36,11 @@ public:
|
||||
Thread& operator=(Thread&) = delete;
|
||||
|
||||
template<typename _Callable, typename... _Args>
|
||||
requires (!std::is_same_v<std::decay_t<_Callable>, Thread>)
|
||||
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 = std::move(__f), ... __args = std::move(__args)]() mutable {
|
||||
if constexpr (!std::is_void_v<std::invoke_result_t<decltype(__f), decltype(__args)...>>) {
|
||||
returnValue = __f(std::move(__args)...);
|
||||
}
|
||||
else {
|
||||
__f(std::move(__args)...); // void 반환은 저장하지 않음
|
||||
}
|
||||
returnValuePtr = new std::invoke_result_t<_Callable, _Args...>(__f(std::move(__args)...));
|
||||
};
|
||||
std::function<void()> funcPtr = std::move(boundFunc);
|
||||
#ifdef _WIN32
|
||||
@@ -53,9 +49,39 @@ public:
|
||||
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 = std::move(__f), ... __args = std::move(__args)]() mutable {
|
||||
__f(std::move(__args)...);
|
||||
};
|
||||
std::function<void()> funcPtr = std::move(boundFunc);
|
||||
#ifdef _WIN32
|
||||
handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, new std::function<void()>(std::move(funcPtr)), 0, nullptr);
|
||||
#elif __linux__
|
||||
pthread_create(&handle_, NULL, thread_func, funcPtr);
|
||||
#endif
|
||||
}*/
|
||||
~Thread();
|
||||
|
||||
void* join();
|
||||
void join() {
|
||||
#ifdef _WIN32
|
||||
WaitForSingleObject(handle_, INFINITE);
|
||||
#elif __linux__
|
||||
pthread_join(handle_, returnValue);
|
||||
#endif
|
||||
}
|
||||
template<typename _RetType>
|
||||
requires (!std::is_void_v<_RetType>)
|
||||
_RetType join() {
|
||||
#ifdef _WIN32
|
||||
WaitForSingleObject(handle_, INFINITE);
|
||||
#elif __linux__
|
||||
pthread_join(handle_, returnValue);
|
||||
#endif
|
||||
return *static_cast<_RetType *>(returnValuePtr);
|
||||
}
|
||||
void detach();
|
||||
|
||||
private:
|
||||
@@ -64,7 +90,7 @@ private:
|
||||
#elif __linux__
|
||||
pthread_t handle_;
|
||||
#endif
|
||||
void* returnValue = nullptr;
|
||||
void* returnValuePtr = nullptr;
|
||||
bool detached = false;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user