TDME2  1.9.200
Barrier.cpp
Go to the documentation of this file.
2 
3 #include <tdme/tdme.h>
6 
7 using std::string;
8 
10 
13 
14 Barrier::~Barrier() {
15  // wait until all threads entered the barrier
16  m.lock();
17  while (entered < count) {
18  c.wait(m);
19  }
20  m.unlock();
21 
22  // spin until all exited
23  while (exited < count) {
24  //
25  }
26 }
27 
28 bool Barrier::wait() {
29  m.lock();
30 
31  // one thread entered the barrier
33 
34  // did we reach barrier?
35  if (entered == count) {
36  // broadcast all threads waiting on condition
37  c.broadcast();
38  m.unlock();
39 
40  // notify exited thread
42 
43  // exit
44  return true;
45  } else {
46  // wait until barrier is reached
47  while (entered < count) {
48  c.wait(m);
49  }
50  m.unlock();
51 
52  // notify exited thread
54 
55  //
56  return false;
57  }
58 }
static uint32_t increment(volatile uint32_t &value, uint32_t byValue=1)
Increment uint32 value and return its value.
Barrier implementation.
Definition: Barrier.h:21
volatile unsigned int exited
Definition: Barrier.h:48
bool wait()
Waits on barrier.
Definition: Barrier.cpp:28
volatile unsigned int entered
Definition: Barrier.h:47
void broadcast()
wake ups all waiting threads on this condition, associated mutex should protect broadcast
Definition: Condition.h:49
void wait(Mutex &mutex)
Blocks current thread until signaled/broadcasted, associated mutex should protect wait.
Definition: Condition.h:56
Mutex implementation.
Definition: Mutex.h:19
void unlock()
Unlocks this mutex.
Definition: Mutex.h:54
void lock()
Locks the mutex, additionally mutex locks will block until other locks have been unlocked.
Definition: Mutex.h:47