]> granicus.if.org Git - icinga2/commitdiff
Don't wait for tasks when destroying a threadpool.
authorGunnar Beutner <gunnar@beutner.name>
Mon, 18 Jun 2012 04:44:26 +0000 (06:44 +0200)
committerGunnar Beutner <gunnar@beutner.name>
Mon, 18 Jun 2012 04:44:26 +0000 (06:44 +0200)
base/threadpool.cpp
base/threadpool.h

index 663014e83e1470e6217bc446b4051e1029a3bb34..2b602b1c5474c957d19c1fef5a74ca16ce5a9acc 100644 (file)
@@ -11,15 +11,17 @@ ThreadPool::ThreadPool(long numThreads)
 
 ThreadPool::~ThreadPool(void)
 {
-       unique_lock<mutex> lock(m_Lock);
-       
-       /* wait for all pending tasks */
-       while (m_Tasks.size() > 0)
-               m_CV.wait(lock);
+       {
+               unique_lock<mutex> 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<mutex> 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<mutex> lock(m_Lock);
 
                        while (m_Tasks.size() == 0) {
-                               m_CV.wait(lock);
-
                                if (!m_Alive)
                                        return;
+
+                               m_CV.wait(lock);
                        }
 
                        task = m_Tasks.front();
index 18c9a84a8a9b0c7a2c1892c21e62c93412a69cfb..11643e144374863df8e66e61e4919aed65b43a76 100644 (file)
@@ -18,6 +18,7 @@ public:
        static ThreadPool::Ptr GetDefaultPool(void);
 
        void EnqueueTask(Task task);
+       void WaitForTasks(void);
 
 private:
        mutex m_Lock;