OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
Public Member Functions | Protected Attributes | List of all members
ossim::Barrier Class Reference

Barrier is a class used to block threads so we can synchronize and entry point. More...

#include <Barrier.h>

Public Member Functions

 Barrier (ossim_int32 n)
 Constructor. More...
 
 ~Barrier ()
 Destructor will reset and release all blocked threads. More...
 
void block ()
 block will block the thread based on a wait condition. More...
 
void reset ()
 Will reset the barrier to the original values. More...
 
void reset (ossim_int32 maxCount)
 Will reset the barrier to a new block count. More...
 
ossim_int32 getMaxCount () const
 
ossim_int32 getBlockedCount () const
 

Protected Attributes

ossim_int32 m_maxCount
 
ossim_int32 m_blockedCount
 
std::atomic< ossim_int32m_waitCount
 
std::mutex m_mutex
 
std::condition_variable m_conditionalBlock
 
std::condition_variable m_conditionalWait
 Will be used for destructing and resetting. More...
 

Detailed Description

Barrier is a class used to block threads so we can synchronize and entry point.

In this example we show how to block the threads so they all start at the same time when executing their work. Example:

int nThreads = 2;
ossim::Barrier barrierStart(nThreads)
// one more for main thread
ossim::Barrier barrierFinished(nThreads+1);
class TestThread : public ossim::Thread
{
public:
TestThread():ossim::Thread(){}
~TestThread(){
waitForCompletion();
}
protected:
virtual void run()
{
barrierStart.block();
for(int x =0 ; x < 10;++x){
std::cout << "THREAD: " << getCurrentThreadId() << "\n";
sleepInMilliSeconds(10);
interrupt();
}
barrierFinished.block();
}
};
int main(int agrc, char* argv[])
{
std::vector<std::shared_ptr<TestThread> > threads(nThreads);
for(auto& thread:threads)
{
thread = std::make_shared<TestThread>();
thread->start();
}
// block main until barrier enters their finished state
barrierFinished.block();
// you can also reset the barriers and run again
barrierFinished.reset();
barrierStart.reset();
for(auto& thread:threads)
{
thread->start();
}
barrierFinished.block();
}

Definition at line 69 of file Barrier.h.

Constructor & Destructor Documentation

◆ Barrier()

ossim::Barrier::Barrier ( ossim_int32  n)

Constructor.

Parameters
nis the number of threads you wish to block

Definition at line 3 of file Barrier.cpp.

4 : m_maxCount(maxCount),
6  m_waitCount(0)
7 {
8 
9 }
ossim_int32 m_blockedCount
Definition: Barrier.h:112
ossim_int32 m_maxCount
Definition: Barrier.h:111
std::atomic< ossim_int32 > m_waitCount
Definition: Barrier.h:113

◆ ~Barrier()

ossim::Barrier::~Barrier ( )

Destructor will reset and release all blocked threads.

Definition at line 11 of file Barrier.cpp.

12 {
13  reset();
14 }
void reset()
Will reset the barrier to the original values.
Definition: Barrier.cpp:34

Member Function Documentation

◆ block()

void ossim::Barrier::block ( )

block will block the thread based on a wait condition.

it will verify if the thread can be blocked by testing if the number of blocked threads is less than the total number to blocked threads. If the total is reached then all threads are notified and woken up and released

Definition at line 16 of file Barrier.cpp.

17 {
18  std::unique_lock<std::mutex> lock(m_mutex);
21  {
22  ++m_waitCount;
23  m_conditionalBlock.wait(lock, [this]{return m_blockedCount>=m_maxCount;} );
24  --m_waitCount;
25  }
26  else
27  {
28  m_conditionalBlock.notify_all();
29  }
30  // always notify the conditional wait just in case anyone is waiting
31  m_conditionalWait.notify_all();
32 }
std::condition_variable m_conditionalBlock
Definition: Barrier.h:115
std::mutex m_mutex
Definition: Barrier.h:114
ossim_int32 m_blockedCount
Definition: Barrier.h:112
ossim_int32 m_maxCount
Definition: Barrier.h:111
std::condition_variable m_conditionalWait
Will be used for destructing and resetting.
Definition: Barrier.h:122
std::atomic< ossim_int32 > m_waitCount
Definition: Barrier.h:113

◆ getBlockedCount()

ossim_int32 ossim::Barrier::getBlockedCount ( ) const

Definition at line 66 of file Barrier.cpp.

67 {
68  std::lock_guard<std::mutex> lock(m_mutex);
69  return m_blockedCount;
70 }
std::mutex m_mutex
Definition: Barrier.h:114
ossim_int32 m_blockedCount
Definition: Barrier.h:112

◆ getMaxCount()

ossim_int32 ossim::Barrier::getMaxCount ( ) const

Definition at line 60 of file Barrier.cpp.

61 {
62  std::lock_guard<std::mutex> lock(m_mutex);
63  return m_maxCount;
64 }
std::mutex m_mutex
Definition: Barrier.h:114
ossim_int32 m_maxCount
Definition: Barrier.h:111

◆ reset() [1/2]

void ossim::Barrier::reset ( )

Will reset the barrier to the original values.

It will also release all blocked threads and wait for their release before resetting.

Definition at line 34 of file Barrier.cpp.

35 {
36  std::unique_lock<std::mutex> lock(m_mutex);
37  // force the condition on any waiting threads
39  if(m_waitCount.load() > 0){
40  m_conditionalBlock.notify_all();
41  // wait until the wait count goes back to zero
42  m_conditionalWait.wait(lock, [this]{return m_waitCount.load()<1;});
43  }
44  // should be safe to reset everything at this point
45  m_blockedCount = 0;
46  m_waitCount = 0;
47 }
std::condition_variable m_conditionalBlock
Definition: Barrier.h:115
std::mutex m_mutex
Definition: Barrier.h:114
ossim_int32 m_blockedCount
Definition: Barrier.h:112
ossim_int32 m_maxCount
Definition: Barrier.h:111
std::condition_variable m_conditionalWait
Will be used for destructing and resetting.
Definition: Barrier.h:122
std::atomic< ossim_int32 > m_waitCount
Definition: Barrier.h:113

◆ reset() [2/2]

void ossim::Barrier::reset ( ossim_int32  maxCount)

Will reset the barrier to a new block count.

It will also release all blocked threads and wait for their release before resetting.

Parameters
maxCountis the max number of threads to block

Definition at line 49 of file Barrier.cpp.

50 {
51  // all threads should be released after this call
52  reset();
53  {
54  // now safe to update the new max count
55  std::unique_lock<std::mutex> lock(m_mutex);
56  m_maxCount = maxCount;
57  }
58 }
std::mutex m_mutex
Definition: Barrier.h:114
ossim_int32 m_maxCount
Definition: Barrier.h:111
void reset()
Will reset the barrier to the original values.
Definition: Barrier.cpp:34

Member Data Documentation

◆ m_blockedCount

ossim_int32 ossim::Barrier::m_blockedCount
protected

Definition at line 112 of file Barrier.h.

◆ m_conditionalBlock

std::condition_variable ossim::Barrier::m_conditionalBlock
protected

Definition at line 115 of file Barrier.h.

◆ m_conditionalWait

std::condition_variable ossim::Barrier::m_conditionalWait
protected

Will be used for destructing and resetting.

resetting should only happen in the main thread

Definition at line 122 of file Barrier.h.

◆ m_maxCount

ossim_int32 ossim::Barrier::m_maxCount
protected

Definition at line 111 of file Barrier.h.

◆ m_mutex

std::mutex ossim::Barrier::m_mutex
mutableprotected

Definition at line 114 of file Barrier.h.

◆ m_waitCount

std::atomic<ossim_int32> ossim::Barrier::m_waitCount
protected

Definition at line 113 of file Barrier.h.


The documentation for this class was generated from the following files: