parallel-cellular-automata
Framework for building parallel cellular automata.
queues.hpp
Go to the documentation of this file.
1 
11 #ifndef PARALLEL_CELLULAR_AUTOMATA_WORKSTEALINGQUEUE_HPP
12 #define PARALLEL_CELLULAR_AUTOMATA_WORKSTEALINGQUEUE_HPP
13 #include <condition_variable>
14 #include <deque>
15 #include <functional>
16 #include <memory>
17 #include <mutex>
18 #include <queue>
19 
25 template <typename T>
27 {
28 
29  public:
35 
42  {
43  std::lock_guard<std::mutex> lock(m);
44  queue = other.queue;
45  }
51  void push(T elem)
52  {
53  std::lock_guard<std::mutex> lock(m);
54  queue.push(elem);
55  cond.notify_one();
56  }
64  bool try_pop(T &result)
65  {
66  std::lock_guard<std::mutex> lock(m);
67  if (queue.empty())
68  {
69  return false;
70  }
71  else
72  {
73  result = std::move(queue.front());
74  queue.pop();
75  return true;
76  }
77  }
83  std::shared_ptr<T> try_pop()
84  {
85  std::lock_guard<std::mutex> lock(m);
86  if (queue.empty())
87  {
88  return std::shared_ptr<T>();
89  }
90  else
91  {
92  auto result = std::make_shared<T>(std::move(queue.front()));
93  queue.pop();
94  return result;
95  }
96  }
102  void pop(T &result)
103  {
104  std::unique_lock<std::mutex> lock(m);
105  cond.wait(lock, [this] { return !queue.empty(); });
106  result = std::move(queue.front());
107  queue.pop();
108  };
114  std::shared_ptr<T> pop()
115  {
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()));
119  queue.pop();
120  return result;
121  }
129  bool empty() const
130  {
131  std::lock_guard<std::mutex> lock(m);
132  return queue.empty();
133  }
134 
140  size_t size() const
141  {
142  std::lock_guard<std::mutex> lock(m);
143  return queue.size();
144  }
145 
146  private:
147  // mutable so it can be locked in empty and in the copy constructor
148  mutable std::mutex m;
149  std::queue<T> queue;
150  std::condition_variable cond;
151 };
152 
163 template <typename T>
165 {
166 
167  public:
173  WorkStealingQueue(const WorkStealingQueue &other) = delete;
179  void push(T data)
180  {
181  std::lock_guard<std::mutex> lock(m);
182  queue.push_front(std::move(data));
183  }
190  bool empty() const
191  {
192  std::lock_guard<std::mutex> lock(m);
193  return queue.empty();
194  }
202  bool try_pop(T &result)
203  {
204  std::lock_guard<std::mutex> lock(m);
205  if (queue.empty())
206  {
207  return false;
208  }
209  result = std::move(queue.front());
210  queue.pop_front();
211  return true;
212  }
220  bool try_steal(T &result)
221  {
222  std::lock_guard<std::mutex> lock(m);
223  if (queue.empty())
224  {
225  return false;
226  }
227  result = std::move(queue.back());
228  queue.pop_back();
229  return true;
230  }
236  size_t size() const
237  {
238  std::lock_guard<std::mutex> lock(m);
239  return queue.size();
240  }
241 
242  private:
243  mutable std::mutex m;
244  std::deque<T> queue;
245 };
246 #endif
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