knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::ThreadPool::Runner Class Referenceabstract

#include <ThreadPool.h>

Inheritance diagram for knowrob::ThreadPool::Runner:

Public Member Functions

 Runner ()
 
virtual ~Runner ()
 
 Runner (const Runner &)=delete
 
void join ()
 
virtual void run ()=0
 
void stop (bool wait)
 
bool hasStopRequest () const
 
bool isTerminated () const
 
 Runner ()
 
virtual ~Runner ()
 
 Runner (const Runner &)=delete
 
void join ()
 
virtual void run ()=0
 
void stop (bool wait)
 
bool hasStopRequest () const
 
bool isTerminated () const
 

Protected Member Functions

void runInternal ()
 
void setExceptionHandler (ExceptionHandler exceptionHandler)
 
void runInternal ()
 
void setExceptionHandler (ExceptionHandler exceptionHandler)
 

Protected Attributes

std::atomic< bool > isTerminated_
 
std::atomic< bool > isRunning_
 
std::atomic< bool > hasStopRequest_
 
std::mutex mutex_
 
std::condition_variable finishedCV_
 
ExceptionHandler exceptionHandler_
 

Friends

class ThreadPool::Worker
 
class ThreadPool
 

Detailed Description

An object that provides a run function which is evaluated in a worker thread.

Definition at line 93 of file ThreadPool.h.

Constructor & Destructor Documentation

◆ Runner() [1/4]

ThreadPool::Runner::Runner ( )

Definition at line 161 of file ThreadPool.cpp.

162  : isTerminated_(false),
163  isRunning_(false),
164  hasStopRequest_(false),
165  exceptionHandler_(nullptr) {}
std::atomic< bool > hasStopRequest_
Definition: ThreadPool.h:133
std::atomic< bool > isRunning_
Definition: ThreadPool.h:132
std::atomic< bool > isTerminated_
Definition: ThreadPool.h:131
ExceptionHandler exceptionHandler_
Definition: ThreadPool.h:136

◆ ~Runner() [1/2]

ThreadPool::Runner::~Runner ( )
virtual

Definition at line 167 of file ThreadPool.cpp.

167  {
168  if (isRunning_) stop(true);
169 }

◆ Runner() [2/4]

knowrob::ThreadPool::Runner::Runner ( const Runner )
delete

Cannot be copy-assigned.

◆ Runner() [3/4]

knowrob::ThreadPool::Runner::Runner ( )

◆ ~Runner() [2/2]

virtual knowrob::ThreadPool::Runner::~Runner ( )
virtual

◆ Runner() [4/4]

knowrob::ThreadPool::Runner::Runner ( const Runner )
delete

Cannot be copy-assigned.

Member Function Documentation

◆ hasStopRequest() [1/2]

bool knowrob::ThreadPool::Runner::hasStopRequest ( ) const
inline
Returns
true if the runner was requested to stop.

Definition at line 123 of file ThreadPool.h.

123 { return hasStopRequest_; }

◆ hasStopRequest() [2/2]

bool knowrob::ThreadPool::Runner::hasStopRequest ( ) const
inline
Returns
true if the runner was requested to stop.

Definition at line 123 of file ThreadPool.h.

123 { return hasStopRequest_; }

◆ isTerminated() [1/2]

bool knowrob::ThreadPool::Runner::isTerminated ( ) const
inline
Returns
true if the runner is still active.

Definition at line 128 of file ThreadPool.h.

128 { return isTerminated_; }

◆ isTerminated() [2/2]

bool knowrob::ThreadPool::Runner::isTerminated ( ) const
inline
Returns
true if the runner is still active.

Definition at line 128 of file ThreadPool.h.

128 { return isTerminated_; }

◆ join() [1/2]

void ThreadPool::Runner::join ( )

Wait until run function has exited.

Definition at line 171 of file ThreadPool.cpp.

171  {
172  if (!isTerminated()) {
173  std::unique_lock<std::mutex> lk(mutex_);
174  finishedCV_.wait(lk, [this] { return isTerminated(); });
175  }
176 }
std::condition_variable finishedCV_
Definition: ThreadPool.h:135

◆ join() [2/2]

void knowrob::ThreadPool::Runner::join ( )

Wait until run function has exited.

◆ run() [1/2]

virtual void knowrob::ThreadPool::Runner::run ( )
pure virtual

◆ run() [2/2]

virtual void knowrob::ThreadPool::Runner::run ( )
pure virtual

◆ runInternal() [1/2]

void ThreadPool::Runner::runInternal ( )
protected

Definition at line 178 of file ThreadPool.cpp.

178  {
179  isRunning_ = true;
180  isTerminated_ = false;
181  hasStopRequest_ = false;
182  // do the work
183  try {
184  run();
185  }
186  catch (const boost::python::error_already_set &) {
187  PythonError py_error;
188  if (exceptionHandler_) {
189  exceptionHandler_(py_error);
190  } else {
191  KB_WARN("Worker error in Python code: {}.", py_error.what());
192  }
193  }
194  catch (const std::exception &e) {
195  if (exceptionHandler_) {
197  } else {
198  KB_WARN("Worker error: {}.", e.what());
199  }
200  }
201  catch (abi::__forced_unwind const &) {
202  // this is a forced unwind, rethrow. this happens when the thread is cancelled.
203  KB_WARN("Worker forced unwind.");
204  throw;
205  }
206  catch (...) {
207  KB_WARN("Unknown worker error.");
208  }
209  // toggle flag
210  isTerminated_ = true;
211  isRunning_ = false;
212  finishedCV_.notify_all();
213 }
#define KB_WARN
Definition: Logger.h:27

◆ runInternal() [2/2]

void knowrob::ThreadPool::Runner::runInternal ( )
protected

◆ setExceptionHandler() [1/2]

void knowrob::ThreadPool::Runner::setExceptionHandler ( ExceptionHandler  exceptionHandler)
inlineprotected

Definition at line 140 of file ThreadPool.h.

140 { exceptionHandler_ = exceptionHandler; }

◆ setExceptionHandler() [2/2]

void knowrob::ThreadPool::Runner::setExceptionHandler ( ExceptionHandler  exceptionHandler)
inlineprotected

Definition at line 140 of file ThreadPool.h.

140 { exceptionHandler_ = exceptionHandler; }

◆ stop() [1/2]

void ThreadPool::Runner::stop ( bool  wait)

Stop the runner.

Parameters
waitcall blocks until runner exited if true.

Definition at line 215 of file ThreadPool.cpp.

215  {
216  // toggle stop request flag on
217  hasStopRequest_ = true;
218  // wait for the runner to be finished if requested
219  if (wait) {
220  join();
221  }
222 }

◆ stop() [2/2]

void knowrob::ThreadPool::Runner::stop ( bool  wait)

Stop the runner.

Parameters
waitcall blocks until runner exited if true.

Friends And Related Function Documentation

◆ ThreadPool

ThreadPool
friend

Definition at line 144 of file ThreadPool.h.

◆ ThreadPool::Worker

Definition at line 142 of file ThreadPool.h.

Member Data Documentation

◆ exceptionHandler_

ExceptionHandler knowrob::ThreadPool::Runner::exceptionHandler_
protected

Definition at line 136 of file ThreadPool.h.

◆ finishedCV_

std::condition_variable knowrob::ThreadPool::Runner::finishedCV_
protected

Definition at line 135 of file ThreadPool.h.

◆ hasStopRequest_

std::atomic< bool > knowrob::ThreadPool::Runner::hasStopRequest_
protected

Definition at line 133 of file ThreadPool.h.

◆ isRunning_

std::atomic< bool > knowrob::ThreadPool::Runner::isRunning_
protected

Definition at line 132 of file ThreadPool.h.

◆ isTerminated_

std::atomic< bool > knowrob::ThreadPool::Runner::isTerminated_
protected

Definition at line 131 of file ThreadPool.h.

◆ mutex_

std::mutex knowrob::ThreadPool::Runner::mutex_
protected

Definition at line 134 of file ThreadPool.h.


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