diff --git a/Server/include/ServerManager/ServerManager.hpp b/Server/include/ServerManager/ServerManager.hpp new file mode 100644 index 0000000..5f22497 --- /dev/null +++ b/Server/include/ServerManager/ServerManager.hpp @@ -0,0 +1,10 @@ +#pragma once + +namespace Chattr { + +class ServerManager { +public: + +}; + +} \ No newline at end of file diff --git a/Server/src/ServerManager/ServerManager.cpp b/Server/src/ServerManager/ServerManager.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Server/src/server.cpp b/Server/src/server.cpp index 0a8c504..fb27a9a 100644 --- a/Server/src/server.cpp +++ b/Server/src/server.cpp @@ -5,7 +5,6 @@ #include "Utils/Thread.hpp" #include "Utils/ThreadPool.hpp" #include "Utils/StringTokenizer.hpp" -#include "Session/Session.hpp" #include "precomp.hpp" @@ -41,8 +40,8 @@ int main() { spdlog::info("PID : {}", pid); - Chattr::ThreadPool threadPool(3); - /*int returnedIntager = 0; + Chattr::ThreadPool threadPool(0); + int returnedIntager = 0; int passvalue = 2; threadPool.enqueueJob([](int& i){ spdlog::info("JobTest"); @@ -53,7 +52,7 @@ int main() { return 1; }, returnedIntager, std::ref(passvalue)); - Sleep(1000);*/ + sleep(10); while (true) { spdlog::info("Waiting for connection..."); diff --git a/impl/Utils/ThreadPool.cpp b/impl/Utils/ThreadPool.cpp index 9bc9695..1e0ed41 100644 --- a/impl/Utils/ThreadPool.cpp +++ b/impl/Utils/ThreadPool.cpp @@ -4,10 +4,20 @@ namespace Chattr { ThreadPool::ThreadPool(std::uint32_t numThreads) { - workers_.reserve(numThreads); + int numCPU = numThreads; + if (numThreads == 0) { +#ifdef _WIN32 + SYSTEM_INFO sysinfo; + GetSystemInfo(&sysinfo); + numCPU = sysinfo.dwNumberOfProcessors; +#elif __linux__ + numCPU = sysconf(_SC_NPROCESSORS_ONLN); +#endif + } + workers_.reserve(numCPU); - while (numThreads--) - workers_.push_back([this]() -> int { this->Worker(); return 1; }); + while (numCPU--) + workers_.push_back([this]() { this->Worker(); }); } ThreadPool::~ThreadPool() { @@ -24,9 +34,9 @@ void* ThreadPool::Worker() { #elif __linux__ pthread_t pid = pthread_self(); #endif + spdlog::info("ThreadPool Worker : {} up.", pid); while (!terminate_) { std::unique_lock lock(jobQueueMutex); - spdlog::info("ThreadPool Worker : {} Waiting for a job", pid); jobQueueCV_.wait(lock, [this]() { return !this->jobs_.empty() || terminate_; }); if (this->jobs_.empty()) return nullptr; diff --git a/include/Utils/ThreadPool.hpp b/include/Utils/ThreadPool.hpp index bd8cadf..cfc5a58 100644 --- a/include/Utils/ThreadPool.hpp +++ b/include/Utils/ThreadPool.hpp @@ -29,6 +29,8 @@ public: auto task = std::packaged_task(std::move(boundFunc)); jobs_.push(std::move(task)); jobQueueCV_.notify_one(); + + return 0; } template requires std::is_void_v>