parallel-cellular-automata
Framework for building parallel cellular automata.
barrier.hpp
Go to the documentation of this file.
1 
11 #ifndef PARALLEL_CELLULAR_AUTOMATA_BARRIER_HPP
12 #define PARALLEL_CELLULAR_AUTOMATA_BARRIER_HPP
13 #include <condition_variable>
14 #include <functional>
15 #include <mutex>
16 #include <thread>
17 #include <vector>
18 
19 namespace ca
20 {
26 class Barrier
27 {
28  private:
29  std::mutex m;
30  std::condition_variable cond;
31  const unsigned n_threads;
32  unsigned count;
33 
34  // to make the barrier reusable first we count down and then up.
35  enum Direction : unsigned char
36  {
37  DECREASING,
38  INCREASING
39  };
40 
41  Direction direction;
42 
43  public:
50  explicit Barrier(unsigned n);
55  void wait();
62  void wait(std::function<void()> fun);
63 
68  void busy_wait();
69 
76  void busy_wait(std::function<void()> fun);
77 };
78 } // namespace ca
79 #endif
Self-resetting synchronization barrier for threads.
Definition: barrier.hpp:27
Barrier(unsigned n)
Construct a new Barrier object to syncronize n threads.
Definition: barrier.cpp:15
void wait()
wait for the other threads to reach the barrier.
Definition: barrier.cpp:23
void busy_wait()
busy wait for other threads to reach the barrier.
Definition: barrier.cpp:101
Namespace of the framework.
Definition: barrier.hpp:20