diff --git a/Source/Core/Common/WorkQueueThread.h b/Source/Core/Common/WorkQueueThread.h index c0a437c168..9f553d0c89 100644 --- a/Source/Core/Common/WorkQueueThread.h +++ b/Source/Core/Common/WorkQueueThread.h @@ -27,6 +27,7 @@ public: { Shutdown(); m_shutdown.Clear(); + m_cancelled.Clear(); m_function = std::move(function); m_thread = std::thread(&WorkQueueThread::ThreadLoop, this); } @@ -34,6 +35,7 @@ public: template void EmplaceItem(Args&&... args) { + if (!m_cancelled.IsSet()) { std::lock_guard lg(m_lock); m_items.emplace(std::forward(args)...); @@ -41,6 +43,24 @@ public: m_wakeup.Set(); } + void Clear() + { + { + std::lock_guard lg(m_lock); + m_items = std::queue(); + } + m_wakeup.Set(); + } + + void Cancel() + { + m_cancelled.Set(); + Clear(); + Shutdown(); + } + + bool IsCancelled() const { return m_cancelled.IsSet(); } + private: void Shutdown() { @@ -81,6 +101,7 @@ private: std::thread m_thread; Common::Event m_wakeup; Common::Flag m_shutdown; + Common::Flag m_cancelled; std::mutex m_lock; std::queue m_items; };