From d58deddbcc222116066a08b00815f7ac5d2efc25 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 6 Aug 2019 18:10:41 +0900 Subject: [PATCH] Fixed #198 --- httplib.h | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/httplib.h b/httplib.h index 9c4276b..08e52fb 100644 --- a/httplib.h +++ b/httplib.h @@ -285,7 +285,7 @@ public: #if CPPHTTPLIB_THREAD_POOL_COUNT > 0 class ThreadPool : public TaskQueue { public: - ThreadPool(size_t n) : shutdown_(false), remaining_(0) { + ThreadPool(size_t n) : shutdown_(false) { while (n) { auto t = std::make_shared(worker(*this)); threads_.push_back(t); @@ -303,25 +303,13 @@ public: } virtual void shutdown() override { - // Handle all remaining jobs... - for (;;) { - std::unique_lock lock(mutex_); - if (jobs_.empty()) break; - cond_.notify_one(); - } - // Stop all worker threads... { std::unique_lock lock(mutex_); shutdown_ = true; - remaining_ = threads_.size(); } - for (;;) { - std::unique_lock lock(mutex_); - if (!remaining_) break; - cond_.notify_all(); - } + cond_.notify_all(); // Join... for (auto t : threads_) { @@ -342,7 +330,7 @@ private: pool_.cond_.wait( lock, [&] { return !pool_.jobs_.empty() || pool_.shutdown_; }); - if (pool_.shutdown_) { break; } + if (pool_.shutdown_ && pool_.jobs_.empty()) { break; } fn = pool_.jobs_.front(); pool_.jobs_.pop_front(); @@ -351,9 +339,6 @@ private: assert(true == (bool)fn); fn(); } - - std::unique_lock lock(pool_.mutex_); - pool_.remaining_--; } ThreadPool &pool_; @@ -364,7 +349,6 @@ private: std::list> jobs_; bool shutdown_; - size_t remaining_; std::condition_variable cond_; std::mutex mutex_;