스레드 재수정

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

@@ -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;