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

ossimJobThreadQueue allows one to instantiate a thread with a shared queue. More...

#include <ossimJobThreadQueue.h>

Inheritance diagram for ossimJobThreadQueue:
ossim::Thread

Public Member Functions

 ossimJobThreadQueue (std::shared_ptr< ossimJobQueue > jqueue=0)
 constructor that allows one to instantiat the thread with a shared job queue. More...
 
virtual ~ossimJobThreadQueue ()
 destructor. More...
 
void setJobQueue (std::shared_ptr< ossimJobQueue > jqueue)
 Sets the shared queue that this thread will be pulling jobs from. More...
 
std::shared_ptr< ossimJobQueuegetJobQueue ()
 
const std::shared_ptr< ossimJobQueuegetJobQueue () const
 
std::shared_ptr< ossimJobcurrentJob ()
 
void cancelCurrentJob ()
 Will cancel the current job. More...
 
bool isValidQueue () const
 
virtual void run ()
 This is method is overriden from the base thread class and is the main entry point of the thread. More...
 
void setDone (bool done)
 Sets the done flag. More...
 
bool isDone () const
 
virtual void cancel ()
 Cancels the thread. More...
 
bool isEmpty () const
 
bool isProcessingJob () const
 
bool hasJobsToProcess () const
 
- Public Member Functions inherited from ossim::Thread
 Thread ()
 Constructor for this thread. More...
 
virtual ~Thread ()
 Destructor for this thread. More...
 
void start ()
 Will actually start the thread and will call the. More...
 
bool isRunning () const
 
bool isInterruptable () const
 This is typically set if. More...
 
virtual void setCancel (bool flag)
 
virtual void waitForCompletion ()
 Convenience to allow one to wait for this thread to finish it's work. More...
 
void pause ()
 Enables the thread to be paused. More...
 
void resume ()
 This will resume a blocked thread. More...
 
bool isPaused () const
 

Protected Member Functions

void startThreadForQueue ()
 Internal method. More...
 
virtual std::shared_ptr< ossimJobnextJob ()
 Will return the next job on the queue. More...
 
- Protected Member Functions inherited from ossim::Thread
virtual void interrupt ()
 This is the interrupt interface and will cause an internal exception that is caught by. More...
 
virtual void runInternal ()
 runInternal sets up internal flags such as setting m_running to true and checks to make sure it's not interrupted and will then call the More...
 

Protected Attributes

bool m_doneFlag
 
std::mutex m_threadMutex
 
std::shared_ptr< ossimJobQueuem_jobQueue
 
std::shared_ptr< ossimJobm_currentJob
 

Additional Inherited Members

- Static Public Member Functions inherited from ossim::Thread
static void sleepInSeconds (ossim_uint64 seconds)
 Utility method to allow one to sleep in seconds. More...
 
static void sleepInMilliSeconds (ossim_uint64 millis)
 Utility method to allow one to sleep in milliseconds. More...
 
static void sleepInMicroSeconds (ossim_uint64 micros)
 Utility method to allow one to sleep in microseconds. More...
 
static std::thread::id getCurrentThreadId ()
 Utility method to get the current thread ID. More...
 
static ossim_uint64 getNumberOfProcessors ()
 Utility to return the number of processors (concurrent threads) More...
 
static void yieldCurrentThread ()
 Will yield the current thread. More...
 

Detailed Description

ossimJobThreadQueue allows one to instantiate a thread with a shared queue.

the thread will block if the queue is empty and will continue to pop jobs off the queue calling the start method on the job. Once it finishes the job it is disguarded and then the next job will be popped off the queue.

class TestJob : public ossimJob
{
public:
TestJob(){}
protected:
virtual void run()
{
}
};
class MyCallback : public ossimJobCallback
{
public:
MyCallback(){}
virtual void started(std::shared_ptr<ossimJob> job)
{
std::cout << "Started job\n";
}
virtual void finished(std::shared_ptr<ossimJob> job)
{
std::cout << "Finished job\n";
}
};
int main(int argc, char *argv[])
{
std::shared_ptr<ossimJobQueue> jobQueue = std::make_shared<ossimJobQueue>();
std::shared_ptr<ossimJobThreadQueue> jobThreadQueue = std::make_shared<ossimJobThreadQueue>(jobQueue);
jobThreadQueue->start();
std::shared_ptr<TestJob> job = std::make_shared<TestJob>();
job->setCallback(std::make_shared<MyCallback>());
jobQueue->add(job);
std::cout << "Waiting 5 seconds before terminating\n";
jobThreadQueue->cancel();
jobThreadQueue->waitForCompletion();
return 0;
}

Definition at line 57 of file ossimJobThreadQueue.h.

Constructor & Destructor Documentation

◆ ossimJobThreadQueue()

ossimJobThreadQueue::ossimJobThreadQueue ( std::shared_ptr< ossimJobQueue jqueue = 0)

constructor that allows one to instantiat the thread with a shared job queue.

Parameters
jqueueshared job queue

Definition at line 3 of file ossimJobThreadQueue.cpp.

References setJobQueue().

4 :m_doneFlag(false)
5 {
6  setJobQueue(jqueue);
7 }
void setJobQueue(std::shared_ptr< ossimJobQueue > jqueue)
Sets the shared queue that this thread will be pulling jobs from.

◆ ~ossimJobThreadQueue()

ossimJobThreadQueue::~ossimJobThreadQueue ( )
virtual

destructor.

Will terminate the thread and stop current jobs

Definition at line 183 of file ossimJobThreadQueue.cpp.

References cancel().

184 {
185  cancel();
186 }
virtual void cancel()
Cancels the thread.

Member Function Documentation

◆ cancel()

void ossimJobThreadQueue::cancel ( )
virtual

Cancels the thread.

Reimplemented from ossim::Thread.

Definition at line 140 of file ossimJobThreadQueue.cpp.

References ossim::Thread::isRunning(), m_currentJob, m_doneFlag, m_jobQueue, m_threadMutex, and ossim::Thread::yieldCurrentThread().

Referenced by ~ossimJobThreadQueue().

141 {
142 
143  if( isRunning() )
144  {
145  {
146  std::lock_guard<std::mutex> lock(m_threadMutex);
147  m_doneFlag = true;
148  if (m_currentJob)
149  {
150  m_currentJob->cancel();
151  }
152 
153  if (m_jobQueue)
154  {
155  m_jobQueue->releaseBlock();
156  }
157  }
158 
159  // then wait for the the thread to stop running.
160  while(isRunning())
161  {
162 #if 1
163  {
164  std::lock_guard<std::mutex> lock(m_threadMutex);
165 
166  if (m_jobQueue)
167  {
168  m_jobQueue->releaseBlock();
169  }
170  }
171 #endif
173  }
174  }
175 }
std::shared_ptr< ossimJobQueue > m_jobQueue
bool isRunning() const
Definition: Thread.h:100
std::shared_ptr< ossimJob > m_currentJob
static void yieldCurrentThread()
Will yield the current thread.
Definition: Thread.cpp:98

◆ cancelCurrentJob()

void ossimJobThreadQueue::cancelCurrentJob ( )

Will cancel the current job.

Definition at line 43 of file ossimJobThreadQueue.cpp.

References m_currentJob, and m_threadMutex.

44 {
45  std::lock_guard<std::mutex> lock(m_threadMutex);
46  if(m_currentJob)
47  {
48  m_currentJob->cancel();
49  }
50 }
std::shared_ptr< ossimJob > m_currentJob

◆ currentJob()

std::shared_ptr< ossimJob > ossimJobThreadQueue::currentJob ( )
Returns
the current job that is being handled.

Definition at line 37 of file ossimJobThreadQueue.cpp.

References m_currentJob, and m_threadMutex.

38 {
39  std::lock_guard<std::mutex> lock(m_threadMutex);
40  return m_currentJob;
41 }
std::shared_ptr< ossimJob > m_currentJob

◆ getJobQueue() [1/2]

std::shared_ptr< ossimJobQueue > ossimJobThreadQueue::getJobQueue ( )
Returns
the current shared job queue

Definition at line 25 of file ossimJobThreadQueue.cpp.

References m_jobQueue, and m_threadMutex.

26 {
27  std::lock_guard<std::mutex> lock(m_threadMutex);
28  return m_jobQueue;
29 }
std::shared_ptr< ossimJobQueue > m_jobQueue

◆ getJobQueue() [2/2]

const std::shared_ptr< ossimJobQueue > ossimJobThreadQueue::getJobQueue ( ) const
Returns
the current shared job queue

Definition at line 31 of file ossimJobThreadQueue.cpp.

References m_jobQueue, and m_threadMutex.

32 {
33  std::lock_guard<std::mutex> lock(m_threadMutex);
34  return m_jobQueue;
35 }
std::shared_ptr< ossimJobQueue > m_jobQueue

◆ hasJobsToProcess()

bool ossimJobThreadQueue::hasJobsToProcess ( ) const
Returns
true if there are still jobs to be processed false otherwise.

Definition at line 199 of file ossimJobThreadQueue.cpp.

References m_currentJob, m_jobQueue, and m_threadMutex.

200 {
201  bool result = false;
202  {
203  std::lock_guard<std::mutex> lock(m_threadMutex);
204  result = (!m_jobQueue->isEmpty()||m_currentJob);
205  }
206 
207  return result;
208 }
std::shared_ptr< ossimJobQueue > m_jobQueue
std::shared_ptr< ossimJob > m_currentJob

◆ isDone()

bool ossimJobThreadQueue::isDone ( ) const
Returns
if the done flag is set

Definition at line 128 of file ossimJobThreadQueue.cpp.

References m_doneFlag, and m_threadMutex.

129 {
130  std::lock_guard<std::mutex> lock(m_threadMutex);
131  return m_doneFlag;
132 }

◆ isEmpty()

bool ossimJobThreadQueue::isEmpty ( ) const
Returns
true if the queue is empty false otherwise.

Definition at line 177 of file ossimJobThreadQueue.cpp.

References m_jobQueue, and m_threadMutex.

178 {
179  std::lock_guard<std::mutex> lock(m_threadMutex);
180  return m_jobQueue->isEmpty();
181 }
std::shared_ptr< ossimJobQueue > m_jobQueue

◆ isProcessingJob()

bool ossimJobThreadQueue::isProcessingJob ( ) const
Returns
true if a job is currently being processed false otherwise.

Definition at line 134 of file ossimJobThreadQueue.cpp.

References m_currentJob, and m_threadMutex.

135 {
136  std::lock_guard<std::mutex> lock(m_threadMutex);
137  return (m_currentJob!=nullptr);
138 }
std::shared_ptr< ossimJob > m_currentJob

◆ isValidQueue()

bool ossimJobThreadQueue::isValidQueue ( ) const
Returns
is the queue valid

Definition at line 51 of file ossimJobThreadQueue.cpp.

References m_jobQueue, and m_threadMutex.

Referenced by run().

52 {
53  std::lock_guard<std::mutex> lock(m_threadMutex);
54  return (m_jobQueue!=nullptr);
55 }
std::shared_ptr< ossimJobQueue > m_jobQueue

◆ nextJob()

std::shared_ptr< ossimJob > ossimJobThreadQueue::nextJob ( )
protectedvirtual

Will return the next job on the queue.

Definition at line 210 of file ossimJobThreadQueue.cpp.

References m_doneFlag, m_jobQueue, and m_threadMutex.

Referenced by run().

211 {
212  std::shared_ptr<ossimJob> job;
213  m_threadMutex.lock();
214  std::shared_ptr<ossimJobQueue> jobQueue = m_jobQueue;
215  bool checkIfValid = !m_doneFlag&&jobQueue;
216  m_threadMutex.unlock();
217  if(checkIfValid)
218  {
219  job = jobQueue->nextJob(true);
220  }
221  return job;
222 }
std::shared_ptr< ossimJobQueue > m_jobQueue

◆ run()

void ossimJobThreadQueue::run ( )
virtual

This is method is overriden from the base thread class and is the main entry point of the thread.

Implements ossim::Thread.

Definition at line 57 of file ossimJobThreadQueue.cpp.

References ossim::Thread::interrupt(), isValidQueue(), m_currentJob, m_doneFlag, m_threadMutex, nextJob(), and ossim::Thread::yieldCurrentThread().

58 {
59  bool firstTime = true;
60  bool validQueue = true;
61  std::shared_ptr<ossimJob> job;
62  do
63  {
64  interrupt();
65  // osg::notify(osg::NOTICE)<<"In thread loop "<<this<<std::endl;
66  validQueue = isValidQueue();
67  job = nextJob();
68  if (job&&!m_doneFlag)
69  {
70  {
71  std::lock_guard<std::mutex> lock(m_threadMutex);
72  m_currentJob = job;
73  }
74 
75  // if the job is ready to execute
76  if(job->isReady())
77  {
78  job->start();
79  }
80  {
81  std::lock_guard<std::mutex> lock(m_threadMutex);
82  m_currentJob = 0;
83  }
84  job.reset();
85  }
86 
87  if (firstTime)
88  {
90  firstTime = false;
91  }
92  } while (!m_doneFlag&&validQueue);
93 
94  {
95  std::lock_guard<std::mutex> lock(m_threadMutex);
96  m_currentJob = 0;
97  }
98  if(job&&m_doneFlag&&job->isReady())
99  {
100  job->cancel();
101  }
102  job = 0;
103 }
virtual void interrupt()
This is the interrupt interface and will cause an internal exception that is caught by...
Definition: Thread.cpp:103
virtual std::shared_ptr< ossimJob > nextJob()
Will return the next job on the queue.
std::shared_ptr< ossimJob > m_currentJob
static void yieldCurrentThread()
Will yield the current thread.
Definition: Thread.cpp:98

◆ setDone()

void ossimJobThreadQueue::setDone ( bool  done)

Sets the done flag.

Parameters
donethe value to set

Definition at line 105 of file ossimJobThreadQueue.cpp.

References m_currentJob, m_doneFlag, m_jobQueue, and m_threadMutex.

106 {
107  m_threadMutex.lock();
108  if (m_doneFlag==done)
109  {
110  m_threadMutex.unlock();
111  return;
112  }
113  m_doneFlag = done;
114  m_threadMutex.unlock();
115  if(done)
116  {
117  {
118  std::lock_guard<std::mutex> lock(m_threadMutex);
119  if (m_currentJob)
120  m_currentJob->release();
121  }
122 
123  if (m_jobQueue)
124  m_jobQueue->releaseBlock();
125  }
126 }
std::shared_ptr< ossimJobQueue > m_jobQueue
std::shared_ptr< ossimJob > m_currentJob

◆ setJobQueue()

void ossimJobThreadQueue::setJobQueue ( std::shared_ptr< ossimJobQueue jqueue)

Sets the shared queue that this thread will be pulling jobs from.

Parameters
jqueuethe shared job queue to set

Definition at line 8 of file ossimJobThreadQueue.cpp.

References ossim::Thread::isPaused(), ossim::Thread::isRunning(), m_jobQueue, m_threadMutex, ossim::Thread::pause(), ossim::Thread::resume(), and startThreadForQueue().

Referenced by ossimJobThreadQueue().

9 {
10  std::lock_guard<std::mutex> lock(m_threadMutex);
11 
12  if (m_jobQueue == jqueue) return;
13 
14  pause();
15  while(isRunning()&&!isPaused())
16  {
17  m_jobQueue->releaseBlock();
18  }
19  m_jobQueue = jqueue;
20  resume();
21 
23 }
std::shared_ptr< ossimJobQueue > m_jobQueue
bool isRunning() const
Definition: Thread.h:100
void resume()
This will resume a blocked thread.
Definition: Thread.cpp:63
void startThreadForQueue()
Internal method.
bool isPaused() const
Definition: Thread.cpp:68
void pause()
Enables the thread to be paused.
Definition: Thread.cpp:58

◆ startThreadForQueue()

void ossimJobThreadQueue::startThreadForQueue ( )
protected

Internal method.

If setJobQueue is set on this thread it will auto start this thread.

Definition at line 188 of file ossimJobThreadQueue.cpp.

References ossim::Thread::isRunning(), m_jobQueue, and ossim::Thread::start().

Referenced by setJobQueue().

189 {
190  if(m_jobQueue)
191  {
192  if(!isRunning())
193  {
194  start();
195  }
196  }
197 }
std::shared_ptr< ossimJobQueue > m_jobQueue
void start()
Will actually start the thread and will call the.
Definition: Thread.cpp:22
bool isRunning() const
Definition: Thread.h:100

Member Data Documentation

◆ m_currentJob

std::shared_ptr<ossimJob> ossimJobThreadQueue::m_currentJob
protected

◆ m_doneFlag

bool ossimJobThreadQueue::m_doneFlag
protected

Definition at line 159 of file ossimJobThreadQueue.h.

Referenced by cancel(), isDone(), nextJob(), run(), and setDone().

◆ m_jobQueue

std::shared_ptr<ossimJobQueue> ossimJobThreadQueue::m_jobQueue
protected

◆ m_threadMutex

std::mutex ossimJobThreadQueue::m_threadMutex
mutableprotected

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