WorkQueueThread: Add flush capability

This commit is contained in:
Scott Mansell 2023-01-23 19:48:07 +13:00
parent aece99fe41
commit 512273a507
1 changed files with 22 additions and 0 deletions

View File

@ -70,6 +70,19 @@ public:
} }
} }
// Doesn't return until the most recent function invocation has finished.
void Flush()
{
if (m_thread.joinable())
{
m_flush.Set();
Clear();
m_flushed.Wait();
}
}
bool IsFlushing() const { return m_flush.IsSet() || m_shutdown.IsSet(); }
private: private:
void ThreadLoop() void ThreadLoop()
{ {
@ -83,7 +96,14 @@ private:
{ {
std::unique_lock lg(m_lock); std::unique_lock lg(m_lock);
if (m_items.empty()) if (m_items.empty())
{
if (m_flush.IsSet())
{
m_flush.Clear();
m_flushed.Set();
}
break; break;
}
T item{std::move(m_items.front())}; T item{std::move(m_items.front())};
m_items.pop(); m_items.pop();
lg.unlock(); lg.unlock();
@ -101,6 +121,8 @@ private:
Common::Event m_wakeup; Common::Event m_wakeup;
Common::Flag m_shutdown; Common::Flag m_shutdown;
Common::Flag m_cancelled; Common::Flag m_cancelled;
Common::Flag m_flush;
Common::Event m_flushed;
std::mutex m_lock; std::mutex m_lock;
std::queue<T> m_items; std::queue<T> m_items;
}; };