From: Gunnar Beutner Date: Mon, 18 Jun 2012 04:44:26 +0000 (+0200) Subject: Don't wait for tasks when destroying a threadpool. X-Git-Tag: v0.0.1~402 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3ebe5534017420c08886ca1eb52236e0cc4f2640;p=icinga2 Don't wait for tasks when destroying a threadpool. --- diff --git a/base/threadpool.cpp b/base/threadpool.cpp index 663014e83..2b602b1c5 100644 --- a/base/threadpool.cpp +++ b/base/threadpool.cpp @@ -11,15 +11,17 @@ ThreadPool::ThreadPool(long numThreads) ThreadPool::~ThreadPool(void) { - unique_lock lock(m_Lock); - - /* wait for all pending tasks */ - while (m_Tasks.size() > 0) - m_CV.wait(lock); + { + unique_lock lock(m_Lock); + + m_Tasks.clear(); - /* notify worker threads to exit */ - m_Alive = false; - m_CV.notify_all(); + /* notify worker threads to exit */ + m_Alive = false; + m_CV.notify_all(); + } + + m_Threads.join_all(); } void ThreadPool::EnqueueTask(Task task) @@ -29,6 +31,15 @@ void ThreadPool::EnqueueTask(Task task) m_CV.notify_one(); } +void ThreadPool::WaitForTasks(void) +{ + unique_lock lock(m_Lock); + + /* wait for all pending tasks */ + while (m_Tasks.size() > 0) + m_CV.wait(lock); +} + void ThreadPool::WorkerThreadProc(void) { while (true) { @@ -38,10 +49,10 @@ void ThreadPool::WorkerThreadProc(void) unique_lock lock(m_Lock); while (m_Tasks.size() == 0) { - m_CV.wait(lock); - if (!m_Alive) return; + + m_CV.wait(lock); } task = m_Tasks.front(); diff --git a/base/threadpool.h b/base/threadpool.h index 18c9a84a8..11643e144 100644 --- a/base/threadpool.h +++ b/base/threadpool.h @@ -18,6 +18,7 @@ public: static ThreadPool::Ptr GetDefaultPool(void); void EnqueueTask(Task task); + void WaitForTasks(void); private: mutex m_Lock;