11 #ifndef PARALLEL_CELLULAR_AUTOMATA_WORKSTEALINGQUEUE_HPP
12 #define PARALLEL_CELLULAR_AUTOMATA_WORKSTEALINGQUEUE_HPP
13 #include <condition_variable>
43 std::lock_guard<std::mutex> lock(m);
53 std::lock_guard<std::mutex> lock(m);
66 std::lock_guard<std::mutex> lock(m);
73 result = std::move(queue.front());
85 std::lock_guard<std::mutex> lock(m);
88 return std::shared_ptr<T>();
92 auto result = std::make_shared<T>(std::move(queue.front()));
104 std::unique_lock<std::mutex> lock(m);
105 cond.wait(lock, [
this] {
return !queue.empty(); });
106 result = std::move(queue.front());
116 std::unique_lock<std::mutex> lock(m);
117 cond.wait(lock, [
this] {
return !queue.empty(); });
118 auto result = std::make_shared<T>(std::move(queue.front()));
131 std::lock_guard<std::mutex> lock(m);
132 return queue.empty();
142 std::lock_guard<std::mutex> lock(m);
148 mutable std::mutex m;
150 std::condition_variable cond;
163 template <
typename T>
181 std::lock_guard<std::mutex> lock(m);
182 queue.push_front(std::move(data));
192 std::lock_guard<std::mutex> lock(m);
193 return queue.empty();
204 std::lock_guard<std::mutex> lock(m);
209 result = std::move(queue.front());
222 std::lock_guard<std::mutex> lock(m);
227 result = std::move(queue.back());
238 std::lock_guard<std::mutex> lock(m);
243 mutable std::mutex m;
A thread-safe wrapper around a queue.
Definition: queues.hpp:27
ThreadSafeQueue()
Construct a new ThreadSafeQueue object.
Definition: queues.hpp:34
ThreadSafeQueue(ThreadSafeQueue const &other)
Construct a new ThreadSafeQueue object.
Definition: queues.hpp:41
bool empty() const
Check if the queue is empty.
Definition: queues.hpp:129
bool try_pop(T &result)
Try to pop an element from the queue.
Definition: queues.hpp:64
size_t size() const
returns the size of the queue. (The value may be outdated)
Definition: queues.hpp:140
void pop(T &result)
Pop an element from the queue.
Definition: queues.hpp:102
void push(T elem)
Insert an element into the queue.
Definition: queues.hpp:51
std::shared_ptr< T > try_pop()
Try to pop an element from the queue.
Definition: queues.hpp:83
std::shared_ptr< T > pop()
Pop an element from the queue.
Definition: queues.hpp:114
work-stealing queue.
Definition: queues.hpp:165
void push(T data)
Push data into the queue.
Definition: queues.hpp:179
bool try_pop(T &result)
Try to pop an element from the front of the queue.
Definition: queues.hpp:202
bool try_steal(T &result)
Try to pop an element from the front of the queue.
Definition: queues.hpp:220
size_t size() const
returns the size of the queue. (The value may be outdated)
Definition: queues.hpp:236
bool empty() const
Check if the queue is empty.
Definition: queues.hpp:190
WorkStealingQueue()
Construct a new WorkStealingQueue object.
Definition: queues.hpp:172