From 89633f94a24c83db425a1e49d46a503b46979e94 Mon Sep 17 00:00:00 2001 From: HappyTanuki Date: Wed, 30 Apr 2025 07:29:07 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9E=84=EC=8B=9C=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/src/server.cpp | 16 ++++++++++++++-- impl/Utils/ThreadPool.cpp | 5 ++--- include/Session/Session.hpp | 4 ++-- include/Utils/Thread.hpp | 29 +++++++++++++---------------- include/Utils/ThreadPool.hpp | 30 ++++++++++++++++++++++++------ 5 files changed, 55 insertions(+), 29 deletions(-) diff --git a/Server/src/server.cpp b/Server/src/server.cpp index 19afe0f..4515379 100644 --- a/Server/src/server.cpp +++ b/Server/src/server.cpp @@ -42,6 +42,18 @@ int main() { Chattr::ThreadPool threadPool(3); + /*int returnedIntager = 0; + int passvalue = 2; + threadPool.enqueueJob([](int& i){ + spdlog::info("JobTest"); + if (i == 2) { + i = 1; + return 2; + } + return 1; + }, returnedIntager, std::ref(passvalue)); + + Sleep(1000);*/ while (true) { spdlog::info("Waiting for connection..."); @@ -49,8 +61,8 @@ int main() { threadPool.enqueueJob(_TCPClient, std::move(clientSock), clientAddr); - //Chattr::Thread thread_(_TCPClient, std::move(clientSock), clientAddr); - //thread_.detach(); + Chattr::Thread thread_(_TCPClient, std::move(clientSock), clientAddr); + thread_.detach(); } } diff --git a/impl/Utils/ThreadPool.cpp b/impl/Utils/ThreadPool.cpp index a8c7381..1f1a449 100644 --- a/impl/Utils/ThreadPool.cpp +++ b/impl/Utils/ThreadPool.cpp @@ -32,13 +32,12 @@ void* ThreadPool::Worker() { if (this->jobs_.empty()) return nullptr; - auto jobPair = std::move(jobs_.front()); + auto job = std::move(jobs_.front()); jobs_.pop(); lock.unlock(); spdlog::info("ThreadPool Worker : {} Executing a job", pid); - auto job = jobPair.first(); - jobPair.second() = (*job)(); + job(); } return nullptr; diff --git a/include/Session/Session.hpp b/include/Session/Session.hpp index d55747d..05455cb 100644 --- a/include/Session/Session.hpp +++ b/include/Session/Session.hpp @@ -16,8 +16,8 @@ public: int recv(); private: - struct std::vector tcpSock_; - struct std::vector udpSock_; + std::vector tcpSock_; + std::vector udpSock_; struct Snowflake sessId_; }; } \ No newline at end of file diff --git a/include/Utils/Thread.hpp b/include/Utils/Thread.hpp index 26de644..9434f5a 100644 --- a/include/Utils/Thread.hpp +++ b/include/Utils/Thread.hpp @@ -12,24 +12,18 @@ namespace Chattr { -template -concept ReturnsVoidPtr = std::is_same_v< - std::invoke_result_t, - void* ->; - class Thread { public: #ifdef _WIN32 static unsigned __stdcall thread_func(LPVOID param) { - // auto task = static_cast*>(param); - std::unique_ptr> 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,16 +36,19 @@ public: Thread& operator=(Thread&) = delete; template - requires ReturnsVoidPtr<_Callable, _Args...> && (!std::is_same_v, Thread>) + requires (!std::is_same_v, Thread>) Thread(_Callable&& __f, _Args&&... __args) { - auto boundFunc = [this, __f = std::move(__f), ... __args = std::move(__args)]() mutable -> void* { - void* ret = __f(std::move(__args)...); - returnValue = ret; - return ret; + 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 반환은 저장하지 않음 + } }; - std::function funcPtr = std::move(boundFunc); + std::function funcPtr = 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, new std::function(std::move(funcPtr)), 0, nullptr); #elif __linux__ pthread_create(&handle_, NULL, thread_func, funcPtr); #endif diff --git a/include/Utils/ThreadPool.hpp b/include/Utils/ThreadPool.hpp index 8c4951f..1d4d700 100644 --- a/include/Utils/ThreadPool.hpp +++ b/include/Utils/ThreadPool.hpp @@ -15,17 +15,35 @@ public: ~ThreadPool(); template - requires ReturnsVoidPtr<_Callable, _Args...> && (!std::is_same_v, Thread>) - int enqueueJob(_Callable&& __job, void* retVal, _Args&&... __args) { + requires (!std::is_void_v>) + int enqueueJob(_Callable&& __job, std::invoke_result_t<_Callable, _Args...>& retVal, _Args&&... __args) { if (terminate_) { spdlog::error("Cannot run jobs on threads that terminating..."); return -1; } std::lock_guard lock(jobQueueMutex); - auto boundFunc = std::bind(std::forward<_Callable>(__job), std::forward<_Args>(__args)...); - std::function job(std::move(boundFunc)); - jobs_.push(job, retVal); + auto boundFunc = [&retVal, __job = std::move(__job), ... __args = std::move(__args)]() mutable { + retVal = __job(std::move(__args)...); + }; + auto task = std::packaged_task(std::move(boundFunc)); + jobs_.push(std::move(task)); + jobQueueCV_.notify_one(); + } + template + requires std::is_void_v> + int enqueueJob(_Callable&& __job, _Args&&... __args) { + if (terminate_) { + spdlog::error("Cannot run jobs on threads that terminating..."); + return -1; + } + + std::lock_guard lock(jobQueueMutex); + auto boundFunc = [__job = std::move(__job), ... __args = std::move(__args)]() mutable { + __job(std::move(__args)...); + }; + auto task = std::packaged_task(std::move(boundFunc)); + jobs_.push(std::move(task)); jobQueueCV_.notify_one(); } @@ -34,7 +52,7 @@ private: std::condition_variable jobQueueCV_; std::mutex jobQueueMutex; - std::queue, void*>> jobs_; + std::queue> jobs_; std::vector workers_; bool terminate_ = false; };