Skip to content

Commit

Permalink
Improvements to WorkQueueThread
Browse files Browse the repository at this point in the history
- Do not use a lambda for std::thread as invoke constructor exists
- Use simpler std::lock_guard wherever possible
- Do not require T to be default constructible
- Move T out of the queue instead of copying
  • Loading branch information
CookiePLMonster committed Oct 8, 2019
1 parent 19ed641 commit 26ebf5b
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions Source/Core/Common/WorkQueueThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ class WorkQueueThread
Shutdown();
m_shutdown.Clear();
m_function = std::move(function);
m_thread = std::thread([this] { ThreadLoop(); });
m_thread = std::thread(&WorkQueueThread::ThreadLoop, this);
}

template <typename... Args>
void EmplaceItem(Args&&... args)
{
{
std::unique_lock<std::mutex> lg(m_lock);
std::lock_guard lg(m_lock);
m_items.emplace(std::forward<Args>(args)...);
}
m_wakeup.Set();
Expand All @@ -59,14 +59,13 @@ class WorkQueueThread

while (true)
{
T item;
{
std::unique_lock<std::mutex> lg(m_lock);
if (m_items.empty())
break;
item = m_items.front();
m_items.pop();
}
std::unique_lock lg(m_lock);
if (m_items.empty())
break;
T item{std::move(m_items.front())};
m_items.pop();
lg.unlock();

m_function(std::move(item));
}

Expand Down

0 comments on commit 26ebf5b

Please sign in to comment.