스레드 재수정

This commit is contained in:
2025-04-30 16:21:51 +09:00
parent 36725fd8f2
commit b662823726
5 changed files with 29 additions and 8 deletions

View File

@@ -0,0 +1,10 @@
#pragma once
namespace Chattr {
class ServerManager {
public:
};
}

View File

@@ -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...");

View File

@@ -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<std::mutex> 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;

View File

@@ -29,6 +29,8 @@ public:
auto task = std::packaged_task<void()>(std::move(boundFunc));
jobs_.push(std::move(task));
jobQueueCV_.notify_one();
return 0;
}
template<typename _Callable, typename... _Args>
requires std::is_void_v<std::invoke_result_t<_Callable, _Args...>>