@@ -64,10 +64,14 @@ class ThreadPool
6464 std::packaged_task<void ()> task;
6565 {
6666 // Wait only if needed; avoid sleeping when a new task was submitted while we were processing another one.
67- if (!m_interrupt && m_work_queue.empty ()) {
67+ // if (!m_interrupt && m_work_queue.empty()) {
6868 // Block until the pool is interrupted or a task is available.
69- m_cv.wait (wait_lock, [&]() EXCLUSIVE_LOCKS_REQUIRED (m_mutex) { return m_interrupt || !m_work_queue.empty (); });
70- }
69+ std::cout << " locking thread: " << std::this_thread::get_id () << std::endl;
70+ m_cv.wait (wait_lock, [&]() EXCLUSIVE_LOCKS_REQUIRED (m_mutex) {
71+ std::cout << " check executed, thread: " << std::this_thread::get_id () << std::endl;
72+ return m_interrupt || !m_work_queue.empty (); });
73+ std::cout << " unlocked thread: " << std::this_thread::get_id () << std::endl;
74+ // }
7175
7276 // If stopped and no work left, exit worker
7377 if (m_interrupt && m_work_queue.empty ()) {
@@ -130,6 +134,7 @@ class ThreadPool
130134 std::vector<std::thread> threads_to_join;
131135 {
132136 LOCK (m_mutex);
137+ if (m_workers.empty ()) return ; // nothing to do
133138 // Ensure 'Stop()' isn't called from any worker thread to avoid deadlocks
134139 auto id = std::this_thread::get_id ();
135140 for (const auto & worker : m_workers) assert (worker.get_id () != id);
@@ -138,6 +143,7 @@ class ThreadPool
138143 threads_to_join.swap (m_workers);
139144 }
140145 m_cv.notify_all ();
146+ std::cout << " post notify" << std::endl;
141147 for (auto & worker : threads_to_join) worker.join ();
142148 // Since we currently wait for tasks completion, sanity-check empty queue
143149 WITH_LOCK (m_mutex, Assume (m_work_queue.empty ()));
0 commit comments