aboutsummaryrefslogtreecommitdiff
path: root/src/common/Threading/ProducerConsumerQueue.h
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2025-11-30 14:25:32 +0100
committerShauren <shauren.trinity@gmail.com>2025-11-30 14:25:32 +0100
commit90be8fafb39469bd2c318c033e63294ebaad2ca4 (patch)
tree2d2d5424e54339b7581f9e224e909d6f08003136 /src/common/Threading/ProducerConsumerQueue.h
parentd3f2aee245d62c70c940831531b17da821053f91 (diff)
Core/Misc: Use std::scoped_lock instead of unique_lock where possible (and old lock_guard)HEADmaster
Diffstat (limited to 'src/common/Threading/ProducerConsumerQueue.h')
-rw-r--r--src/common/Threading/ProducerConsumerQueue.h21
1 files changed, 9 insertions, 12 deletions
diff --git a/src/common/Threading/ProducerConsumerQueue.h b/src/common/Threading/ProducerConsumerQueue.h
index 04c2017c1f7..7efa24cb78c 100644
--- a/src/common/Threading/ProducerConsumerQueue.h
+++ b/src/common/Threading/ProducerConsumerQueue.h
@@ -18,10 +18,10 @@
#ifndef TRINITY_PRODUCER_CONSUMER_QUEUE_H
#define TRINITY_PRODUCER_CONSUMER_QUEUE_H
+#include <atomic>
#include <condition_variable>
#include <mutex>
#include <queue>
-#include <atomic>
#include <type_traits>
template <typename T>
@@ -39,7 +39,7 @@ public:
void Push(T const& value)
{
- std::lock_guard<std::mutex> lock(_queueLock);
+ std::scoped_lock lock(_queueLock);
_queue.push(value);
_condition.notify_one();
@@ -47,7 +47,7 @@ public:
void Push(T&& value)
{
- std::lock_guard<std::mutex> lock(_queueLock);
+ std::scoped_lock lock(_queueLock);
_queue.push(std::move(value));
_condition.notify_one();
@@ -55,21 +55,21 @@ public:
bool Empty() const
{
- std::lock_guard<std::mutex> lock(_queueLock);
+ std::scoped_lock lock(_queueLock);
return _queue.empty();
}
size_t Size() const
{
- std::lock_guard<std::mutex> lock(_queueLock);
+ std::scoped_lock lock(_queueLock);
return _queue.size();
}
bool Pop(T& value)
{
- std::lock_guard<std::mutex> lock(_queueLock);
+ std::scoped_lock lock(_queueLock);
if (_queue.empty() || _shutdown)
return false;
@@ -83,12 +83,9 @@ public:
void WaitAndPop(T& value)
{
- std::unique_lock<std::mutex> lock(_queueLock);
+ std::unique_lock lock(_queueLock);
- // we could be using .wait(lock, predicate) overload here but it is broken
- // https://connect.microsoft.com/VisualStudio/feedback/details/1098841
- while (_queue.empty() && !_shutdown)
- _condition.wait(lock);
+ _condition.wait(lock, [&] { return !_queue.empty() || _shutdown; });
if (_queue.empty() || _shutdown)
return;
@@ -100,7 +97,7 @@ public:
void Cancel()
{
- std::unique_lock<std::mutex> lock(_queueLock);
+ std::scoped_lock lock(_queueLock);
while (!_queue.empty())
{