임시저장(빌드 안됨) 졸려서 자러갈거야

This commit is contained in:
2025-04-29 21:49:58 +09:00
parent 1c6404530d
commit 1703974310
8 changed files with 167 additions and 17 deletions

View File

@@ -12,39 +12,53 @@
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::packaged_task<void()>*>(param));
// auto task = static_cast<std::function<void *()>*>(param);
std::unique_ptr<std::function<void*()>> 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::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>) //복사 생성하면 안 되므로
requires ReturnsVoidPtr<_Callable, _Args...> && (!std::is_same_v<std::decay_t<_Callable>, Thread>)
Thread(_Callable&& __f, _Args&&... __args) {
auto boundFunc = [__f = std::move(__f), ... __args = std::move(__args)]() mutable {
__f(std::move(__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 funcPtr = new std::packaged_task<void()>(std::move(boundFunc));
std::function<void* ()> funcPtr = std::move(boundFunc);
#ifdef _WIN32
handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, 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
}
~Thread();
void join();
void* join();
void detach();
private:
@@ -53,6 +67,7 @@ private:
#elif __linux__
pthread_t handle_;
#endif
void* returnValue = nullptr;
bool detached = false;
};