임시저장

This commit is contained in:
2025-04-30 07:29:07 +09:00
parent 1703974310
commit 89633f94a2
5 changed files with 55 additions and 29 deletions

View File

@@ -12,24 +12,18 @@
namespace Chattr {
template<typename F, typename... Args>
concept ReturnsVoidPtr = std::is_same_v<
std::invoke_result_t<F, Args...>,
void*
>;
class Thread {
public:
#ifdef _WIN32
static unsigned __stdcall thread_func(LPVOID param) {
// auto task = static_cast<std::function<void *()>*>(param);
std::unique_ptr<std::function<void*()>> task(static_cast<std::function<void* ()>*>(param));
auto task = static_cast<std::function<void()>*>(param);
(*task)();
delete task;
return 0;
}
#elif __linux__
static void* thread_func(void *param) {
auto task(static_cast<std::packaged_task<void*()>*>(param));
auto task = static_cast<std::function<void* ()>*>(param);
(*task)();
delete task;
return 0;
@@ -42,16 +36,19 @@ public:
Thread& operator=(Thread&) = delete;
template<typename _Callable, typename... _Args>
requires ReturnsVoidPtr<_Callable, _Args...> && (!std::is_same_v<std::decay_t<_Callable>, Thread>)
requires (!std::is_same_v<std::decay_t<_Callable>, Thread>)
Thread(_Callable&& __f, _Args&&... __args) {
auto boundFunc = [this, __f = std::move(__f), ... __args = std::move(__args)]() mutable -> void* {
void* ret = __f(std::move(__args)...);
returnValue = ret;
return ret;
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 반환은 저장하지 않음
}
};
std::function<void* ()> funcPtr = std::move(boundFunc);
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);
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