Extended WorkQueueThread class with Clear(), Cancel() and IsCancelled().

This commit is contained in:
Christian Aguilera 2020-08-16 01:33:30 +01:00
parent 5b757024c4
commit 213610e95d

View file

@ -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 <typename... Args>
void EmplaceItem(Args&&... args)
{
if (!m_cancelled.IsSet())
{
std::lock_guard lg(m_lock);
m_items.emplace(std::forward<Args>(args)...);
@ -41,6 +43,24 @@ public:
m_wakeup.Set();
}
void Clear()
{
{
std::lock_guard lg(m_lock);
m_items = std::queue<T>();
}
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<T> m_items;
};