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