From cf8166f41cfd28461d179c75f73de90fe0bfb6df Mon Sep 17 00:00:00 2001 From: HappyTanuki Date: Wed, 30 Apr 2025 15:11:33 +0900 Subject: [PATCH] =?UTF-8?q?=EB=A6=AC=EB=88=85=EC=8A=A4=EC=97=90=EC=84=9C?= =?UTF-8?q?=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=ED=95=B4=EB=B3=BC=20=EA=B2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/src/server.cpp | 4 +++- include/Utils/Thread.hpp | 16 ++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Server/src/server.cpp b/Server/src/server.cpp index 8111694..a9cd093 100644 --- a/Server/src/server.cpp +++ b/Server/src/server.cpp @@ -59,10 +59,12 @@ int main() { spdlog::info("Waiting for connection..."); sock.accept(clientSock, clientAddr); - // threadPool.enqueueJob(_TCPClient, std::move(clientSock), clientAddr); + threadPool.enqueueJob(_TCPClient, std::move(clientSock), clientAddr); Chattr::Thread thread_(_TCPClient, std::move(clientSock), clientAddr); thread_.detach(); + + Sleep(10000000); } } diff --git a/include/Utils/Thread.hpp b/include/Utils/Thread.hpp index b6e3442..8035585 100644 --- a/include/Utils/Thread.hpp +++ b/include/Utils/Thread.hpp @@ -16,14 +16,14 @@ class Thread { public: #ifdef _WIN32 static unsigned __stdcall thread_func(LPVOID param) { - auto task = static_cast*>(param); + auto task = static_cast*>(param); (*task)(); delete task; return 0; } #elif __linux__ static void* thread_func(void *param) { - auto task = static_cast*>(param); + auto task = static_cast*>(param); (*task)(); delete task; return 0; @@ -42,27 +42,27 @@ public: auto boundFunc = [this, __f = std::move(__f), ... __args = std::move(__args)]() mutable { returnValuePtr = new std::invoke_result_t<_Callable, _Args...>(__f(std::move(__args)...)); }; - std::function funcPtr = std::move(boundFunc); + std::packaged_task* funcPtr = new std::packaged_task(std::move(boundFunc)); #ifdef _WIN32 - handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, new std::function(std::move(funcPtr)), 0, nullptr); + handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, funcPtr, 0, nullptr); #elif __linux__ pthread_create(&handle_, NULL, thread_func, funcPtr); #endif } - /*template + template requires (!std::is_same_v, Thread>) && std::is_void_v> Thread(_Callable&& __f, _Args&&... __args) { auto boundFunc = [this, __f = std::move(__f), ... __args = std::move(__args)]() mutable { __f(std::move(__args)...); }; - std::function funcPtr = std::move(boundFunc); + std::packaged_task* funcPtr = new std::packaged_task(std::move(boundFunc)); #ifdef _WIN32 - handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, new std::function(std::move(funcPtr)), 0, nullptr); + handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, funcPtr, 0, nullptr); #elif __linux__ pthread_create(&handle_, NULL, thread_func, funcPtr); #endif - }*/ + } ~Thread(); void join() {