From 0caa9b7c66d9aca2da726cc5c8546f01dc2e11cd Mon Sep 17 00:00:00 2001 From: c8luka Date: Sun, 27 Jun 2021 22:29:40 +0800 Subject: [PATCH] add wait_for time to let thread to wake up for while --- .gitignore | 3 +++ ThreadPool.h | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0a9f9d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.build* + +.ccls_cache* diff --git a/ThreadPool.h b/ThreadPool.h index 4183203..a51136c 100644 --- a/ThreadPool.h +++ b/ThreadPool.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -44,10 +45,12 @@ inline ThreadPool::ThreadPool(size_t threads) { std::unique_lock lock(this->queue_mutex); - this->condition.wait(lock, + this->condition.wait_for(lock, std::chrono::milliseconds(200), [this]{ return this->stop || !this->tasks.empty(); }); - if(this->stop && this->tasks.empty()) + if(this->stop) return; + if(this->tasks.empty()) + continue; task = std::move(this->tasks.front()); this->tasks.pop(); }