OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
Block.cpp
Go to the documentation of this file.
1 #include <ossim/base/Block.h>
2 #include <iostream>
3 
4 ossim::Block::Block(bool releaseFlag)
5 :m_release(releaseFlag),
6 m_waitCount(0)
7 {
8 
9 }
10 
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 }
22 
23 void ossim::Block::set(bool releaseFlag)
24 {
25  {
26  std::unique_lock<std::mutex> lock(m_mutex);
27 
28  m_release = releaseFlag;
29  }
30  m_conditionVariable.notify_all();
31 }
32 
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 }
48 
49 void ossim::Block::block(ossim_uint64 waitTimeMillis)
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 }
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 }
77 
79 {
80  std::unique_lock<std::mutex> lock(m_mutex);
81 
82  m_release = false;
83  m_waitCount = 0;
84 }
void set(bool releaseFlag)
Will set the relase flag and wake up all threads to test the condition again.
Definition: Block.cpp:23
void release()
Releases the threads and will not return until all threads are released.
Definition: Block.cpp:66
unsigned long long ossim_uint64
~Block()
Destructor.
Definition: Block.cpp:11
void block()
Will block the calling thread based on the internal condition.
Definition: Block.cpp:33
Block(bool releaseFlag=false)
Allows one the construct a Block with a release state.
Definition: Block.cpp:4
void reset()
Simple reset the values.
Definition: Block.cpp:78