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

This is a very simple block interface. More...

#include <Block.h>

Public Member Functions

 Block (bool releaseFlag=false)
 Allows one the construct a Block with a release state. More...
 
 ~Block ()
 Destructor. More...
 
void set (bool releaseFlag)
 Will set the relase flag and wake up all threads to test the condition again. More...
 
void block ()
 Will block the calling thread based on the internal condition. More...
 
void block (ossim_uint64 waitTimeMillis)
 Will block the calling thread base on the internal condition. More...
 
void release ()
 Releases the threads and will not return until all threads are released. More...
 
void reset ()
 Simple reset the values. More...
 

Private Attributes

std::mutex m_mutex
 Used by the conditions. More...
 
std::atomic< bool > m_release
 The release state. More...
 
std::condition_variable m_conditionVariable
 Condition that tests the release state. More...
 
std::atomic< ossim_int32m_waitCount
 Used to count the number of threads blocked or waiting on the condition. More...
 
std::condition_variable m_conditionalWait
 Will be used for destructing and releasing. More...
 

Detailed Description

This is a very simple block interface.

This allows one to control how their threads are blocked

There is a release state flag that tells the call to block to block the calling thread or release the thread(s) that are currently blocked

For a very simple use case we will start a thread and call block and have the main sleep for 2 seconds before releasing the thread

std::shared_ptr<ossim::Block> block = std::make_shared<ossim::Block>();
class TestThread : public ossim::Thread
{
public:
protected:
virtual void run(){
block->block();
std::cout << "STARING!!!!!!!\n";
}
};
int main(int argc, char *argv[])
{
TestThread t1;
t1.start();
std::cout << "WAITING 2 SECOND to release block\n";
block->release();
}

Definition at line 45 of file Block.h.

Constructor & Destructor Documentation

◆ Block()

ossim::Block::Block ( bool  releaseFlag = false)

Allows one the construct a Block with a release state.

Definition at line 4 of file Block.cpp.

5 :m_release(releaseFlag),
7 {
8 
9 }
std::atomic< bool > m_release
The release state.
Definition: Block.h:100
std::atomic< ossim_int32 > m_waitCount
Used to count the number of threads blocked or waiting on the condition.
Definition: Block.h:111

◆ ~Block()

ossim::Block::~Block ( )

Destructor.

Will set internally call release

Definition at line 11 of file Block.cpp.

12 {
13  release();
14  {
15  std::unique_lock<std::mutex> lock(m_mutex);
16  if(m_waitCount>0)
17  {
18  m_conditionalWait.wait(lock, [this]{return m_waitCount.load()<1;});
19  }
20  }
21 }
std::atomic< ossim_int32 > m_waitCount
Used to count the number of threads blocked or waiting on the condition.
Definition: Block.h:111
std::mutex m_mutex
Used by the conditions.
Definition: Block.h:95
void release()
Releases the threads and will not return until all threads are released.
Definition: Block.cpp:66
std::condition_variable m_conditionalWait
Will be used for destructing and releasing.
Definition: Block.h:118

Member Function Documentation

◆ block() [1/2]

void ossim::Block::block ( )

Will block the calling thread based on the internal condition.

If the internal condition is set to release then it will return without blocking.

Definition at line 33 of file Block.cpp.

Referenced by ossimMultiThreadSequencer::getNextTile(), ossimMultiThreadSequencer::nextJob(), and ossimJobQueue::nextJob().

34 {
35  std::unique_lock<std::mutex> lock(m_mutex);
36  if(!m_release)
37  {
38  ++m_waitCount;
39  m_conditionVariable.wait(lock, [this]{
40  return (m_release.load() == true);
41  });
42  --m_waitCount;
43  if(m_waitCount < 0) m_waitCount = 0;
44  }
45  m_conditionVariable.notify_all();
46  m_conditionalWait.notify_all();
47 }
std::atomic< bool > m_release
The release state.
Definition: Block.h:100
std::condition_variable m_conditionVariable
Condition that tests the release state.
Definition: Block.h:105
std::atomic< ossim_int32 > m_waitCount
Used to count the number of threads blocked or waiting on the condition.
Definition: Block.h:111
std::mutex m_mutex
Used by the conditions.
Definition: Block.h:95
std::condition_variable m_conditionalWait
Will be used for destructing and releasing.
Definition: Block.h:118

◆ block() [2/2]

void ossim::Block::block ( ossim_uint64  waitTimeMillis)

Will block the calling thread base on the internal condition.

If the internal condition is set to release the it will return without blocking. If the internal condition is set to not release then it will block for the specified time in milliseconds

Parameters
waitTimeMillisspecifies the amount of time to wait for the release

Definition at line 49 of file Block.cpp.

50 {
51  std::unique_lock<std::mutex> lock(m_mutex);
52  if(!m_release)
53  {
54  ++m_waitCount;
55  m_conditionVariable.wait_for(lock,
56  std::chrono::milliseconds(waitTimeMillis),
57  [this]{
58  return (m_release.load() == true);
59  });
60  --m_waitCount;
61  if(m_waitCount < 0) m_waitCount = 0;
62  }
63  m_conditionVariable.notify_all();
64  m_conditionalWait.notify_all();
65 }
std::atomic< bool > m_release
The release state.
Definition: Block.h:100
std::condition_variable m_conditionVariable
Condition that tests the release state.
Definition: Block.h:105
std::atomic< ossim_int32 > m_waitCount
Used to count the number of threads blocked or waiting on the condition.
Definition: Block.h:111
std::mutex m_mutex
Used by the conditions.
Definition: Block.h:95
std::condition_variable m_conditionalWait
Will be used for destructing and releasing.
Definition: Block.h:118

◆ release()

void ossim::Block::release ( )

Releases the threads and will not return until all threads are released.

Definition at line 66 of file Block.cpp.

Referenced by ossimMultiThreadSequencer::getNextTile(), ossimJobQueue::releaseBlock(), and ossimMultiThreadSequencer::ossimGetTileJob::run().

67 {
68  {
69  std::unique_lock<std::mutex> lock(m_mutex);
70  if(!m_release)
71  {
72  m_release = true;
73  }
74  m_conditionVariable.notify_all();
75  }
76 }
std::atomic< bool > m_release
The release state.
Definition: Block.h:100
std::condition_variable m_conditionVariable
Condition that tests the release state.
Definition: Block.h:105
std::mutex m_mutex
Used by the conditions.
Definition: Block.h:95

◆ reset()

void ossim::Block::reset ( )

Simple reset the values.

Will not do any releasing

Definition at line 78 of file Block.cpp.

Referenced by ossimMultiThreadSequencer::getNextTile(), and ossimMultiThreadSequencer::nextJob().

79 {
80  std::unique_lock<std::mutex> lock(m_mutex);
81 
82  m_release = false;
83  m_waitCount = 0;
84 }
std::atomic< bool > m_release
The release state.
Definition: Block.h:100
std::atomic< ossim_int32 > m_waitCount
Used to count the number of threads blocked or waiting on the condition.
Definition: Block.h:111
std::mutex m_mutex
Used by the conditions.
Definition: Block.h:95

◆ set()

void ossim::Block::set ( bool  releaseFlag)

Will set the relase flag and wake up all threads to test the condition again.

Definition at line 23 of file Block.cpp.

Referenced by ossimJobQueue::add(), ossimJobQueue::nextJob(), ossimJobQueue::removeById(), and ossimJobQueue::removeByName().

24 {
25  {
26  std::unique_lock<std::mutex> lock(m_mutex);
27 
28  m_release = releaseFlag;
29  }
30  m_conditionVariable.notify_all();
31 }
std::atomic< bool > m_release
The release state.
Definition: Block.h:100
std::condition_variable m_conditionVariable
Condition that tests the release state.
Definition: Block.h:105
std::mutex m_mutex
Used by the conditions.
Definition: Block.h:95

Member Data Documentation

◆ m_conditionalWait

std::condition_variable ossim::Block::m_conditionalWait
private

Will be used for destructing and releasing.

resetting should only happen in the main thread

Definition at line 118 of file Block.h.

◆ m_conditionVariable

std::condition_variable ossim::Block::m_conditionVariable
private

Condition that tests the release state.

Definition at line 105 of file Block.h.

◆ m_mutex

std::mutex ossim::Block::m_mutex
mutableprivate

Used by the conditions.

Definition at line 95 of file Block.h.

◆ m_release

std::atomic<bool> ossim::Block::m_release
private

The release state.

Definition at line 100 of file Block.h.

◆ m_waitCount

std::atomic<ossim_int32> ossim::Block::m_waitCount
private

Used to count the number of threads blocked or waiting on the condition.

Definition at line 111 of file Block.h.


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