Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

Barrier.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/basic.h>
35 
36 #include <mutex>
37 #include <condition_variable>
38 
39 
40 namespace ogdf {
41 
43 
50 class Barrier {
51 
52  std::condition_variable m_allThreadsReachedSync;
54 
55  uint32_t m_threadCount;
57  uint32_t m_syncNumber;
58 
59 public:
60 
62  explicit Barrier(uint32_t numThreads) : m_threadCount(numThreads) {
63  m_numThreadsReachedSync = 0;
64  m_syncNumber = 0;
65  }
66 
68 
72  void threadSync() {
73  std::unique_lock<std::mutex> lk(m_numThreadsReachedSyncLock);
74 
75  uint32_t syncNr = m_syncNumber;
76  m_numThreadsReachedSync++;
77  if (m_numThreadsReachedSync == m_threadCount) {
78  m_syncNumber++;
79  m_allThreadsReachedSync.notify_all();
80  m_numThreadsReachedSync = 0;
81 
82  } else {
83  m_allThreadsReachedSync.wait(lk, [syncNr,this]{return syncNr != m_syncNumber; });
84  }
85  }
86 
87 };
88 
89 
90 } // end of namespace ogdf
uint32_t m_threadCount
the number of threads in the group.
Definition: Barrier.h:55
std::condition_variable m_allThreadsReachedSync
Definition: Barrier.h:52
uint32_t m_numThreadsReachedSync
number of htreads that reached current synchronization point.
Definition: Barrier.h:56
Representation of a barrier.
Definition: Barrier.h:50
void threadSync()
Synchronizes the threads in the group.
Definition: Barrier.h:72
std::mutex m_numThreadsReachedSyncLock
Definition: Barrier.h:53
uint32_t m_syncNumber
number of current synchronization point.
Definition: Barrier.h:57
Basic declarations, included by all source files.
Barrier(uint32_t numThreads)
Creates a barrier for a group of numThreads threads.
Definition: Barrier.h:62
The namespace for all OGDF objects.