일단 거의 모든 버그 다 고칢..
This commit is contained in:
@@ -11,11 +11,7 @@ ThreadPool::ThreadPool(std::uint32_t numThreads) {
|
||||
}
|
||||
|
||||
ThreadPool::~ThreadPool() {
|
||||
terminate_ = true;
|
||||
jobQueueCV_.notify_all();
|
||||
|
||||
for (auto& t : workers_)
|
||||
t.join();
|
||||
terminate();
|
||||
}
|
||||
|
||||
void ThreadPool::init(std::uint32_t numThreads) {
|
||||
@@ -29,12 +25,36 @@ void ThreadPool::init(std::uint32_t numThreads) {
|
||||
numCPU = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
#endif
|
||||
spdlog::info("Auto-detected cpu count: {}", numCPU);
|
||||
spdlog::info("Set ThreadPool Worker count to: {}", numCPU);
|
||||
if (numCPU == 1 || numCPU == 2) {
|
||||
numCPU = 4;
|
||||
spdlog::info("Set ThreadPool Worker count to: {} due to program to oprate concurrently", numCPU);
|
||||
}
|
||||
else {
|
||||
spdlog::info("Set ThreadPool Worker count to: {}", numCPU);
|
||||
}
|
||||
}
|
||||
threadCount = numCPU;
|
||||
workers_.reserve(numCPU);
|
||||
|
||||
while (numCPU--)
|
||||
workers_.push_back([this]() { this->Worker(); });
|
||||
workers_.push_back([this]() {
|
||||
this->Worker();
|
||||
});
|
||||
}
|
||||
|
||||
void ThreadPool::terminate() {
|
||||
terminate_ = true;
|
||||
jobQueueCV_.notify_all();
|
||||
|
||||
spdlog::debug("waiting for threads to end their jobs...");
|
||||
for (auto& t : workers_)
|
||||
t.join();
|
||||
}
|
||||
|
||||
void ThreadPool::respawnWorker(std::uint32_t numThreads) {
|
||||
terminate();
|
||||
terminate_ = false;
|
||||
init(numThreads);
|
||||
}
|
||||
|
||||
void* ThreadPool::Worker() {
|
||||
@@ -43,18 +63,17 @@ 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);
|
||||
jobQueueCV_.wait(lock, [this]() { return !this->jobs_.empty() || terminate_; });
|
||||
if (this->jobs_.empty())
|
||||
return nullptr;
|
||||
break;
|
||||
|
||||
auto job = std::move(jobs_.front());
|
||||
jobs_.pop();
|
||||
lock.unlock();
|
||||
|
||||
spdlog::info("ThreadPool Worker : {} Executing a job", pid);
|
||||
spdlog::debug("ThreadPool Worker : {} Executing a job", pid);
|
||||
job();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user