diff options
| author | Silent <zdanio95@gmail.com> | 2019-10-08 22:57:33 +0200 |
|---|---|---|
| committer | Silent <zdanio95@gmail.com> | 2019-10-08 22:57:33 +0200 |
| commit | 26ebf5b65088062e4f3290b89a973fa3ef3eee1a (patch) | |
| tree | c64494847e0eca75b40e121f4c65eb1d5547a854 /Source/Core | |
| parent | 19ed64122c11f7cd36ff92b61e7af7bb772c01a8 (diff) | |
Improvements to WorkQueueThread
- 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
Diffstat (limited to 'Source/Core')
| -rw-r--r-- | Source/Core/Common/WorkQueueThread.h | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/Source/Core/Common/WorkQueueThread.h b/Source/Core/Common/WorkQueueThread.h index 426d8b493d..e5dfac8b49 100644 --- a/Source/Core/Common/WorkQueueThread.h +++ b/Source/Core/Common/WorkQueueThread.h @@ -27,14 +27,14 @@ public: 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(); @@ -59,14 +59,13 @@ private: 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)); } |
