OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
Thread.cpp
Go to the documentation of this file.
1 #include <ossim/base/Thread.h>
2 
4 :m_running(false),
5  m_interrupt(false),
6  m_pauseBarrier(std::make_shared<ossim::Barrier>(1))
7 {
8 }
9 
11 {
12  if(m_thread)
13  {
14  // we will wait as a sanity but this should be done in derived Thread
15  waitForCompletion();
16  if(m_thread->joinable()) m_thread->join();
17  m_thread = 0;
18  }
19  m_running = false;
20 }
21 
23 {
24  if(isInterruptable()||isRunning()) return;
25  m_running = true;
26 
27  // we are managing the thread internal. If we are not running then we may need to join
28  // before allocating a new thread
29  if(m_thread)
30  {
31  if(m_thread->joinable()) m_thread->join();
32  }
33 
34  m_thread = std::make_shared<std::thread>(&Thread::runInternal, this);
35 }
36 
37 void ossim::Thread::setCancel(bool flag)
38 {
39  setInterruptable(flag);
40  if(flag)
41  {
42  // if it was paused we will resume. Calling resume
43  // will reset the barrier so we can cancel the process
44  //
45  resume();
46  }
47 }
48 
50 {
51  if(m_thread)
52  {
53  std::unique_lock<std::mutex> lock(m_runningMutex);
54  m_runningCondition.wait(lock, [&]{return !isRunning();} );
55  }
56 }
57 
59 {
60  m_pauseBarrier->reset(2);
61 }
62 
64 {
65  m_pauseBarrier->reset(1);
66 }
67 
69 {
70  return (m_pauseBarrier->getBlockedCount()>0);
71 }
72 
74 {
75  std::this_thread::sleep_for(std::chrono::seconds(seconds));
76 }
77 
79 {
80  std::this_thread::sleep_for(std::chrono::milliseconds(millis));
81 }
82 
84 {
85  std::this_thread::sleep_for(std::chrono::microseconds(micros));
86 }
87 
89 {
90  return std::thread::hardware_concurrency();
91 }
92 
94 {
95  return std::this_thread::get_id();
96 }
97 
99 {
100  std::this_thread::yield();
101 }
102 
104 {
105  if(m_interrupt)
106  {
107  throw ossim::Thread::Interrupt();
108  }
109  m_pauseBarrier->block();
110 }
111 
113 {
114  m_interrupt.store(flag, std::memory_order_relaxed);
115 }
116 
118 {
119  try
120  {
121  if(!isInterruptable())
122  {
123  run();
124  }
125  }
126  catch(Interrupt& e)
127  {
128  }
129  m_running = false;
130  m_runningCondition.notify_all();
131 }
132 
static void sleepInMilliSeconds(ossim_uint64 millis)
Utility method to allow one to sleep in milliseconds.
Definition: Thread.cpp:78
void setInterruptable(bool flag)
Definition: Thread.cpp:112
virtual void waitForCompletion()
Convenience to allow one to wait for this thread to finish it&#39;s work.
Definition: Thread.cpp:49
virtual void runInternal()
runInternal sets up internal flags such as setting m_running to true and checks to make sure it&#39;s not...
Definition: Thread.cpp:117
This code was derived from https://gist.github.com/mshockwave.
Definition: Barrier.h:8
virtual void interrupt()
This is the interrupt interface and will cause an internal exception that is caught by...
Definition: Thread.cpp:103
void start()
Will actually start the thread and will call the.
Definition: Thread.cpp:22
This is an Interrupt exception that is thrown if the.
Definition: Thread.h:73
static void sleepInMicroSeconds(ossim_uint64 micros)
Utility method to allow one to sleep in microseconds.
Definition: Thread.cpp:83
virtual ~Thread()
Destructor for this thread.
Definition: Thread.cpp:10
Barrier is a class used to block threads so we can synchronize and entry point.
Definition: Barrier.h:69
unsigned long long ossim_uint64
static ossim_uint64 getNumberOfProcessors()
Utility to return the number of processors (concurrent threads)
Definition: Thread.cpp:88
void resume()
This will resume a blocked thread.
Definition: Thread.cpp:63
virtual void setCancel(bool flag)
Definition: Thread.cpp:37
static void yieldCurrentThread()
Will yield the current thread.
Definition: Thread.cpp:98
static void sleepInSeconds(ossim_uint64 seconds)
Utility method to allow one to sleep in seconds.
Definition: Thread.cpp:73
Thread()
Constructor for this thread.
Definition: Thread.cpp:3
static std::thread::id getCurrentThreadId()
Utility method to get the current thread ID.
Definition: Thread.cpp:93
bool isPaused() const
Definition: Thread.cpp:68
void pause()
Enables the thread to be paused.
Definition: Thread.cpp:58