diff --git a/Server/src/server.cpp b/Server/src/server.cpp index 4515379..8111694 100644 --- a/Server/src/server.cpp +++ b/Server/src/server.cpp @@ -16,9 +16,9 @@ int main() { auto config = Chattr::ConfigManager::load(); Chattr::log::setDefaultLogger(config.logLevel, config.logFileName, config.logfileSize, config.logfileCount); - struct Chattr::TCPSocket sock; + Chattr::TCPSocket sock; struct Chattr::Address serveraddr; - struct Chattr::TCPSocket clientSock; + Chattr::TCPSocket clientSock; struct Chattr::Address clientAddr; if (config.ipVersion == 4) { sock.init(AF_INET); @@ -59,7 +59,7 @@ 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(); diff --git a/impl/Utils/Thread.cpp b/impl/Utils/Thread.cpp index 147753e..7fbb60b 100644 --- a/impl/Utils/Thread.cpp +++ b/impl/Utils/Thread.cpp @@ -16,15 +16,8 @@ Thread::~Thread() { spdlog::critical("There is not joined thread"); std::exit(EXIT_FAILURE); } -} - -void* Thread::join() { -#ifdef _WIN32 - WaitForSingleObject(handle_, INFINITE); -#elif __linux__ - pthread_join(handle_, returnValue); -#endif - return returnValue; + if (returnValuePtr != nullptr) + delete returnValuePtr; } void Thread::detach() { diff --git a/impl/Utils/ThreadPool.cpp b/impl/Utils/ThreadPool.cpp index 1f1a449..f573fc2 100644 --- a/impl/Utils/ThreadPool.cpp +++ b/impl/Utils/ThreadPool.cpp @@ -7,7 +7,7 @@ ThreadPool::ThreadPool(std::uint32_t numThreads) { workers_.reserve(numThreads); while (numThreads--) - workers_.push_back([this]() -> void* { return this->Worker(); }); + workers_.push_back([this]() -> int { this->Worker(); return 1; }); } ThreadPool::~ThreadPool() { @@ -24,7 +24,6 @@ void* ThreadPool::Worker() { #elif __linux__ pid_t pid = getpid(); #endif - spdlog::info("ThreadPool Worker : {}", pid); while (!terminate_) { std::unique_lock lock(jobQueueMutex); spdlog::info("ThreadPool Worker : {} Waiting for a job", pid); diff --git a/include/Utils/Thread.hpp b/include/Utils/Thread.hpp index 9434f5a..b6e3442 100644 --- a/include/Utils/Thread.hpp +++ b/include/Utils/Thread.hpp @@ -23,7 +23,7 @@ public: } #elif __linux__ static void* thread_func(void *param) { - auto task = static_cast*>(param); + auto task = static_cast*>(param); (*task)(); delete task; return 0; @@ -36,15 +36,11 @@ public: Thread& operator=(Thread&) = delete; template - requires (!std::is_same_v, Thread>) + 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 { - if constexpr (!std::is_void_v>) { - returnValue = __f(std::move(__args)...); - } - else { - __f(std::move(__args)...); // void 반환은 저장하지 않음 - } + returnValuePtr = new std::invoke_result_t<_Callable, _Args...>(__f(std::move(__args)...)); }; std::function funcPtr = std::move(boundFunc); #ifdef _WIN32 @@ -53,9 +49,39 @@ public: pthread_create(&handle_, NULL, thread_func, funcPtr); #endif } + /*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); +#ifdef _WIN32 + handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, new std::function(std::move(funcPtr)), 0, nullptr); +#elif __linux__ + pthread_create(&handle_, NULL, thread_func, funcPtr); +#endif + }*/ ~Thread(); - void* join(); + void join() { +#ifdef _WIN32 + WaitForSingleObject(handle_, INFINITE); +#elif __linux__ + pthread_join(handle_, returnValue); +#endif + } + template + requires (!std::is_void_v<_RetType>) + _RetType join() { +#ifdef _WIN32 + WaitForSingleObject(handle_, INFINITE); +#elif __linux__ + pthread_join(handle_, returnValue); +#endif + return *static_cast<_RetType *>(returnValuePtr); + } void detach(); private: @@ -64,7 +90,7 @@ private: #elif __linux__ pthread_t handle_; #endif - void* returnValue = nullptr; + void* returnValuePtr = nullptr; bool detached = false; };