This commit is contained in:
2025-04-26 16:17:19 +09:00
parent c660c41657
commit d13a9ca474
12 changed files with 199 additions and 87 deletions

View File

@@ -8,6 +8,7 @@
#error "이 플랫폼은 지원되지 않습니다."
#endif
#include <functional>
#include <future>
namespace Chattr {
@@ -15,23 +16,26 @@ class Thread {
public:
#ifdef _WIN32
static unsigned __stdcall thread_func(LPVOID param) {
std::unique_ptr<std::function<void()>> func(static_cast<std::function<void()>*>(param));
(*func)();
auto task(static_cast<std::packaged_task<void()>*>(param));
(*task)();
delete task;
return 0;
}
#elif __linux__
static void* thread_func(void *param) {
std::unique_ptr<std::function<void()>> func(static_cast<std::function<void()>*>(param));
(*func)();
auto task(static_cast<std::packaged_task<void()>*>(param));
(*task)();
delete task;
return 0;
}
#endif
template<typename Callable, typename... Args>
Thread(Callable&& f, Args&&... args) {
auto boundFunc = std::bind(std::forward<Callable>(f), std::forward<Args>(args)...);
auto funcPtr = new std::function<void()>(boundFunc);
template<typename _Callable, typename... _Args>
requires (!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 funcPtr = new std::packaged_task<void()>(std::move(boundFunc));
#ifdef _WIN32
handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, funcPtr, 0, nullptr);
#elif __linux__