diff --git a/base/thread_pool_computational.hpp b/base/thread_pool_computational.hpp index 97feb0e9ac..56392fe062 100644 --- a/base/thread_pool_computational.hpp +++ b/base/thread_pool_computational.hpp @@ -76,7 +76,7 @@ public: if (m_done) return {}; - m_queue.emplace(std::move(task)); + m_queue.emplace(std::move(task)); } m_condition.notify_one(); return result; @@ -115,6 +115,16 @@ public: m_condition.notify_all(); } + void WaitAndStop() + { + { + std::unique_lock lock(m_mutex); + m_done = true; + } + m_condition.notify_all(); + m_joiner.TryJoin(); + } + private: void Worker() { diff --git a/base/thread_safe_queue.hpp b/base/thread_safe_queue.hpp index c8b40972af..c5fe8f5584 100644 --- a/base/thread_safe_queue.hpp +++ b/base/thread_safe_queue.hpp @@ -64,7 +64,7 @@ public: { std::unique_lock lk(m_mutex); m_cond.wait(lk, [this]{ return !m_queue.empty(); }); - value = m_queue.front(); + value = std::move(m_queue.front()); m_queue.pop(); } @@ -74,7 +74,7 @@ public: if (m_queue.empty()) return false; - value = m_queue.front(); + value = std::move(m_queue.front()); m_queue.pop(); return true; diff --git a/base/thread_utils.hpp b/base/thread_utils.hpp index 342aab43b0..d0680de47d 100644 --- a/base/thread_utils.hpp +++ b/base/thread_utils.hpp @@ -15,7 +15,7 @@ class ThreadsJoiner public: explicit ThreadsJoiner(ThreadContainer & threads) : m_threads(threads) {} - ~ThreadsJoiner() + void TryJoin() { for (auto & thread : m_threads) { @@ -24,6 +24,11 @@ public: } } + ~ThreadsJoiner() + { + TryJoin(); + } + private: ThreadContainer & m_threads; };