VOX
A little voxel engine
Loading...
Searching...
No Matches
ThreadSafeQueue.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <queue>
4#include <mutex>
5#include "Tracy.hpp"
6
7
8template <typename T>
10{
11public:
14
15 ThreadSafeQueue(const ThreadSafeQueue& other) = delete;
16 ThreadSafeQueue& operator=(const ThreadSafeQueue& other) = delete;
17
19 {
20 std::scoped_lock lock(m_mutex, other.m_mutex);
21 m_queue = std::move(other.m_queue);
22 }
24 {
25 std::scoped_lock lock(m_mutex, other.m_mutex);
26 m_queue = std::move(other.m_queue);
27 return *this;
28 }
29
30 void push(const T& value)
31 {
32 std::lock_guard lock(m_mutex);
33 m_queue.push(value);
34 }
35
36 void push(T&& value)
37 {
38 std::lock_guard lock(m_mutex);
39 m_queue.push(std::move(value));
40 }
41
42 bool empty() const
43 {
44 std::lock_guard lock(m_mutex);
45 return m_queue.empty();
46 }
47
48 size_t size() const
49 {
50 std::lock_guard lock(m_mutex);
51 return m_queue.size();
52 }
53
54 T pop()
55 {
56 std::lock_guard lock(m_mutex);
57 if (m_queue.empty())
58 throw std::out_of_range("Queue is empty");
59 auto value = m_queue.front();
60 m_queue.pop();
61 return value;
62 }
63private:
64 std::queue<T> m_queue;
65 mutable TracyLockableN(std::mutex, m_mutex, "Thread Safe Queue Mutex");
66};
Definition: ThreadSafeQueue.hpp:10
ThreadSafeQueue()
Definition: ThreadSafeQueue.hpp:12
ThreadSafeQueue(const ThreadSafeQueue &other)=delete
T pop()
Definition: ThreadSafeQueue.hpp:54
bool empty() const
Definition: ThreadSafeQueue.hpp:42
void push(const T &value)
Definition: ThreadSafeQueue.hpp:30
ThreadSafeQueue & operator=(const ThreadSafeQueue &other)=delete
size_t size() const
Definition: ThreadSafeQueue.hpp:48
ThreadSafeQueue(ThreadSafeQueue &&other)
Definition: ThreadSafeQueue.hpp:18
~ThreadSafeQueue()
Definition: ThreadSafeQueue.hpp:13
ThreadSafeQueue & operator=(ThreadSafeQueue &&other)
Definition: ThreadSafeQueue.hpp:23
void push(T &&value)
Definition: ThreadSafeQueue.hpp:36