From b07de42a48d5c2c0b2672aacb76cc6a19333561c Mon Sep 17 00:00:00 2001 From: Victor Zarubkin Date: Sun, 25 Feb 2018 21:19:23 +0300 Subject: [PATCH] #0 [Gui] Thread pool fix: there was a possibility that not all tasks would be executed --- profiler_gui/thread_pool.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/profiler_gui/thread_pool.cpp b/profiler_gui/thread_pool.cpp index cb6c194..50fb8f9 100644 --- a/profiler_gui/thread_pool.cpp +++ b/profiler_gui/thread_pool.cpp @@ -115,15 +115,23 @@ void ThreadPool::work() if (m_interrupt.load(std::memory_order_acquire)) break; - if (m_tasks.empty()) - continue; + while (true) // execute all available tasks + { + if (m_tasks.empty()) + break; // the lock will be released on the outer loop new iteration - auto& task = m_tasks.front().get(); - task.setStatus(TaskStatus::Processing); - m_tasks.pop_front(); + auto& task = m_tasks.front().get(); + task.setStatus(TaskStatus::Processing); + m_tasks.pop_front(); - lock.unlock(); + // unlock to permit tasks execution for other worker threads + lock.unlock(); - task.execute(); + // execute task + task.execute(); + + // lock again to check if there are new tasks in the queue + lock.lock(); + } } }