Skip to content

Commit

Permalink
Extended WorkQueueThread class with Clear(), Cancel() and IsCancelled().
Browse files Browse the repository at this point in the history
  • Loading branch information
cristian64 committed Oct 1, 2020
1 parent 5b75702 commit 213610e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Source/Core/Common/WorkQueueThread.h
Expand Up @@ -27,20 +27,40 @@ class WorkQueueThread
{
Shutdown();
m_shutdown.Clear();
m_cancelled.Clear();
m_function = std::move(function);
m_thread = std::thread(&WorkQueueThread::ThreadLoop, this);
}

template <typename... Args>
void EmplaceItem(Args&&... args)
{
if (!m_cancelled.IsSet())
{
std::lock_guard lg(m_lock);
m_items.emplace(std::forward<Args>(args)...);
}
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()
{
Expand Down Expand Up @@ -81,6 +101,7 @@ class WorkQueueThread
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;
};
Expand Down

0 comments on commit 213610e

Please sign in to comment.