Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Wait until task queue is empty in threadpool shutdown.

Kishore Nallan9f90c39b8453
diff --git a/include/threadpool.h b/include/threadpool.h
index dcfa4df4..6382ebd6 100644
--- a/include/threadpool.h
+++ b/include/threadpool.h
@@ -21,6 +21,7 @@ private:
// synchronization
std::mutex queue_mutex;
std::condition_variable condition;
+ std::condition_variable condition_producers;
bool stop;
};
@@ -48,6 +49,9 @@ inline ThreadPool::ThreadPool(size_t threads)
}
task = std::move(this->tasks.front());
this->tasks.pop();
+ if (tasks.empty()) {
+ condition_producers.notify_one(); // notify the destructor that the queue is empty
+ }
}
task();
@@ -82,6 +86,7 @@ decltype(auto) ThreadPool::enqueue(F&& f, Args&&... args)
inline void ThreadPool::shutdown() {
{
std::unique_lock<std::mutex> lock(queue_mutex);
+ condition_producers.wait(lock, [this] { return tasks.empty(); });
stop = true;
}
condition.notify_all();