From 07cff49813d357168772e259072e7ee8154e1245 Mon Sep 17 00:00:00 2001 From: HappyTanuki Date: Thu, 24 Apr 2025 21:15:28 +0900 Subject: [PATCH] =?UTF-8?q?=EB=A9=80=ED=8B=B0=ED=94=8C=EB=9E=AB=ED=8F=BC?= =?UTF-8?q?=20=EC=8A=A4=EB=A0=88=EB=93=9C=20=EB=9D=BC=EC=9D=B4=EB=B8=8C?= =?UTF-8?q?=EB=9F=AC=EB=A6=AC=20=EC=A4=80=20=EC=99=84=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/src/server.cpp | 2 +- impl/Utils/Thread.cpp | 13 +++++++++++++ include/Utils/Thread.hpp | 31 ++++++++++++++++--------------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/Server/src/server.cpp b/Server/src/server.cpp index 3974b0b..c8ef60b 100644 --- a/Server/src/server.cpp +++ b/Server/src/server.cpp @@ -44,5 +44,5 @@ int main() { }); spdlog::info("Waiting for connection..."); - sock.accept(clientSock, clientAddr); + // sock.accept(clientSock, clientAddr); } \ No newline at end of file diff --git a/impl/Utils/Thread.cpp b/impl/Utils/Thread.cpp index 679ebac..207a74b 100644 --- a/impl/Utils/Thread.cpp +++ b/impl/Utils/Thread.cpp @@ -1,2 +1,15 @@ #include "Utils/Thread.hpp" +namespace Chattr { +Thread::~Thread() { + join(); +} + +void Thread::join() { +#ifdef _WIN32 + WaitForSingleObject(handle_, INFINITE); +#elif __linux__ + pthread_join(handle_, NULL); +#endif +} +} \ No newline at end of file diff --git a/include/Utils/Thread.hpp b/include/Utils/Thread.hpp index e99d4c8..4e7ba26 100644 --- a/include/Utils/Thread.hpp +++ b/include/Utils/Thread.hpp @@ -1,6 +1,7 @@ #pragma once #ifdef _WIN32 #include +#include #elif __linux__ #include #else @@ -13,7 +14,7 @@ namespace Chattr { class Thread { public: #ifdef _WIN32 - static DWORD WINAPI thread_func(LPVOID param) { + static unsigned __stdcall thread_func(LPVOID param) { std::unique_ptr> func(static_cast*>(param)); (*func)(); return 0; @@ -26,7 +27,20 @@ public: } #endif template - Thread(Callable&& f, Args&&... args); + Thread(Callable&& f, Args&&... args) { + auto boundFunc = std::bind(std::forward(f), std::forward(args)...); + + auto funcPtr = new std::function(boundFunc); + +#ifdef _WIN32 + handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, funcPtr, 0, nullptr); +#elif __linux__ + pthread_create(&handle_, NULL, thread_func, funcPtr); +#endif + } + ~Thread(); + + void join(); private: #ifdef _WIN32 @@ -36,17 +50,4 @@ private: #endif }; -template -Thread::Thread(Callable&& f, Args&&... args) { - auto boundFunc = std::bind(std::forward(f), std::forward(args)...); - - auto funcPtr = new std::function(boundFunc); - -#ifdef _WIN32 - handle_ = CreateThread(nullptr, 0, thread_func, funcPtr, 0, nullptr); -#elif __linux__ - pthread_create(&handle_, NULL, thread_func, funcPtr); -#endif -} - } // namespace Chattr \ No newline at end of file