OpenWalnut  1.5.0dev
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
WThreadedRunner Class Reference

Base class for all classes needing to be executed in a separate thread. More...

#include <WThreadedRunner.h>

+ Inheritance diagram for WThreadedRunner:
+ Collaboration diagram for WThreadedRunner:

Public Types

typedef std::shared_ptr< WThreadedRunnerSPtr
 Abbreviation to a shared_ptr to this type. More...
 
typedef std::shared_ptr< const WThreadedRunnerConstSPtr
 Abbreviation to a const shared_ptr to this type. More...
 
typedef boost::function< void(void) > THREADFUNCTION
 Type used for simple thread functions. More...
 

Public Member Functions

 WThreadedRunner ()
 Default constructor. More...
 
virtual ~WThreadedRunner ()
 Destructor. More...
 
virtual void run ()
 Run thread. More...
 
void run (THREADFUNCTION f)
 Run thread. More...
 
void wait (bool requestFinish=false)
 Wait for the thread to be finished. More...
 
virtual void requestStop ()
 This method's purpose is to request a stop without waiting for it. More...
 
virtual boost::signals2::connection subscribeSignal (THREAD_SIGNAL signal, t_ThreadErrorSignalHandlerType notifier)
 Connects a specified notify function with a signal this thread instance is offering. More...
 
const WBoolFlagisCrashed () const
 Checks whether this thread has been crashed. More...
 
const std::string & getCrashMessage () const
 Get the message of the exception finally causing the crash. More...
 
void setThreadName (std::string name)
 Set the name of the thread. More...
 
std::string getThreadName () const
 Returns the current thread name. More...
 

Static Public Member Functions

static void setThisThreadName (std::string name)
 Static function to set the name of the calling thread. More...
 

Protected Member Functions

virtual void threadMain ()
 Function that has to be overwritten for execution. More...
 
virtual void notifyStop ()
 Gets called when the thread should be stopped. More...
 
void yield () const
 Give remaining execution timeslice to another thread. More...
 
void sleep (const int32_t t) const
 Sets thread asleep. More...
 
void msleep (const int32_t t) const
 Sets thread asleep. More...
 
void waitForStop ()
 Let the thread sleep until a stop request was given. More...
 
virtual void onThreadException (const WException &e)
 This method is called if an exception was caught, which came from the custom thread code. More...
 
void handleDeadlyException (const WException &e, std::string sender="WThreadedRunner")
 Handle the specified exception which was not caught in the thread, which basically means the thread has crashed. More...
 

Protected Attributes

boost::thread m_thread
 Thread instance. More...
 
WBoolFlag m_shutdownFlag
 Condition getting fired whenever the thread should quit. More...
 
WBoolFlag m_isCrashed
 True whenever an exception is thrown during threadMain. More...
 
std::string m_crashMessage
 The crash message. More...
 

Private Member Functions

 WThreadedRunner (const WThreadedRunner &rhs)
 Disallow copy construction. More...
 
WThreadedRunneroperator= (const WThreadedRunner &rhs)
 Disallow copy assignment. More...
 
void threadMainSave ()
 The is the thread entry point. More...
 

Private Attributes

t_ThreadErrorSignalType signal_thread_error
 Signal fired whenever a thread throws an exception/error. More...
 
std::string m_threadName
 This threads name. More...
 

Detailed Description

Base class for all classes needing to be executed in a separate thread.

Definition at line 43 of file WThreadedRunner.h.

Member Typedef Documentation

◆ ConstSPtr

typedef std::shared_ptr< const WThreadedRunner > WThreadedRunner::ConstSPtr

Abbreviation to a const shared_ptr to this type.

Definition at line 54 of file WThreadedRunner.h.

◆ SPtr

typedef std::shared_ptr< WThreadedRunner > WThreadedRunner::SPtr

Abbreviation to a shared_ptr to this type.

Definition at line 49 of file WThreadedRunner.h.

◆ THREADFUNCTION

typedef boost::function< void ( void ) > WThreadedRunner::THREADFUNCTION

Type used for simple thread functions.

Definition at line 59 of file WThreadedRunner.h.

Constructor & Destructor Documentation

◆ WThreadedRunner() [1/2]

WThreadedRunner::WThreadedRunner ( )

Default constructor.

Definition at line 39 of file WThreadedRunner.cpp.

◆ ~WThreadedRunner()

WThreadedRunner::~WThreadedRunner ( )
virtual

Destructor.

Definition at line 48 of file WThreadedRunner.cpp.

◆ WThreadedRunner() [2/2]

WThreadedRunner::WThreadedRunner ( const WThreadedRunner rhs)
private

Disallow copy construction.

Parameters
rhsthe other threaded runner.

Member Function Documentation

◆ getCrashMessage()

const std::string & WThreadedRunner::getCrashMessage ( ) const

Get the message of the exception finally causing the crash.

Returns
the message

Definition at line 172 of file WThreadedRunner.cpp.

References m_crashMessage.

◆ getThreadName()

std::string WThreadedRunner::getThreadName ( ) const

Returns the current thread name.

Returns
the name, empty if no name was specified.

Definition at line 167 of file WThreadedRunner.cpp.

References m_threadName.

Referenced by threadMainSave().

+ Here is the caller graph for this function:

◆ handleDeadlyException()

void WThreadedRunner::handleDeadlyException ( const WException e,
std::string  sender = "WThreadedRunner" 
)
protected

Handle the specified exception which was not caught in the thread, which basically means the thread has crashed.

This triggers the error notification and marks the thread as crashed. If you write your own exception/error mechanism (like WModule), you should take care that these method gets called.

Note
this method does not re-throw the exception
you should specify a custom sender string if you overwrite onThreadException.
Parameters
ethe exception
senderallows to customize the sender information in the log entry created by this method.

Definition at line 63 of file WThreadedRunner.cpp.

References wlog::error(), m_crashMessage, m_isCrashed, signal_thread_error, and WException::what().

Referenced by onThreadException(), WModule::onThreadException(), and WProjectFile::onThreadException().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isCrashed()

const WBoolFlag & WThreadedRunner::isCrashed ( ) const

Checks whether this thread has been crashed.

This will be true whenever the code in the thread throws an unhandled exception.

Returns
true if there has been an exception during threadMain().

Definition at line 53 of file WThreadedRunner.cpp.

References m_isCrashed.

◆ msleep()

void WThreadedRunner::msleep ( const int32_t  t) const
protected

Sets thread asleep.

Parameters
ttime to sleep in microseconds.

Definition at line 145 of file WThreadedRunner.cpp.

◆ notifyStop()

void WThreadedRunner::notifyStop ( )
protectedvirtual

Gets called when the thread should be stopped.

The purpose of this method is to allow derived classes to handle this kind of event.

Reimplemented in WGraphicsEngine.

Definition at line 131 of file WThreadedRunner.cpp.

Referenced by requestStop().

+ Here is the caller graph for this function:

◆ onThreadException()

void WThreadedRunner::onThreadException ( const WException e)
protectedvirtual

This method is called if an exception was caught, which came from the custom thread code.

This method is virtual and allows you to overwrite the default behaviour. If you overwrite this method, you should call handleDeadlyException or WThreadedRunner::onThreadException if you are finished with your customized code.

Parameters
ethe exception that was caught.

Reimplemented in WProjectFile, and WModule.

Definition at line 58 of file WThreadedRunner.cpp.

References handleDeadlyException().

Referenced by threadMainSave().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator=()

WThreadedRunner& WThreadedRunner::operator= ( const WThreadedRunner rhs)
private

Disallow copy assignment.

Parameters
rhsthe other threaded runner.
Returns
this.

◆ requestStop()

void WThreadedRunner::requestStop ( )
virtual

This method's purpose is to request a stop without waiting for it.

Definition at line 94 of file WThreadedRunner.cpp.

References m_shutdownFlag, and notifyStop().

Referenced by WModuleContainer::moduleError(), WWorkerThreadTest::testStopThread(), and wait().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ run() [1/2]

void WThreadedRunner::run ( )
virtual

◆ run() [2/2]

void WThreadedRunner::run ( THREADFUNCTION  f)

Run thread.

This does not start threadMain(() but runs a specified function instead.

Parameters
fthe function to run instead of the threadMain method.

Definition at line 80 of file WThreadedRunner.cpp.

References m_thread.

◆ setThisThreadName()

void WThreadedRunner::setThisThreadName ( std::string  name)
static

Static function to set the name of the calling thread.

Parameters
namethe name.

Definition at line 184 of file WThreadedRunner.cpp.

Referenced by threadMainSave().

+ Here is the caller graph for this function:

◆ setThreadName()

void WThreadedRunner::setThreadName ( std::string  name)

Set the name of the thread.

This can be handy for debugging as it gets set on Linux as the pthread name. You MUST set this before starting the thread.

Parameters
namethe name

Definition at line 162 of file WThreadedRunner.cpp.

References m_threadName.

Referenced by WModule::initialize(), WGraphicsEngine::WGraphicsEngine(), and WKernel::WKernel().

+ Here is the caller graph for this function:

◆ sleep()

void WThreadedRunner::sleep ( const int32_t  t) const
protected

Sets thread asleep.

Parameters
ttime to sleep in seconds.

Definition at line 140 of file WThreadedRunner.cpp.

Referenced by WModuleImpl::moduleMain(), WMOpenIGTLinkSender::moduleMain(), WMTemplate::moduleMain(), WThreadedRunnerTest::testSleep(), and WThreadedRunnerImpl::threadMain().

+ Here is the caller graph for this function:

◆ subscribeSignal()

boost::signals2::connection WThreadedRunner::subscribeSignal ( THREAD_SIGNAL  signal,
t_ThreadErrorSignalHandlerType  notifier 
)
virtual

Connects a specified notify function with a signal this thread instance is offering.

Exceptions
WSignalSubscriptionFailedthrown if the signal can't be connected.
Parameters
signalthe signal to connect to.
notifierthe notifier function to bind.
Returns
connection descriptor.

Definition at line 150 of file WThreadedRunner.cpp.

References signal_thread_error.

◆ threadMain()

void WThreadedRunner::threadMain ( )
protectedvirtual

Function that has to be overwritten for execution.

It gets executed in a separate thread after run() has been called.

Reimplemented in WIGTLinkRemote, WTuringTextureCreator::TextureThread, WSphericalHarmonicsCoefficientsThread< T >, WTuringPatternCreator::PatternThread, WProjectFile, WModuleCombiner, WModule, WKernel, WKdTreeThread, WBatchLoader, WGraphicsEngine, WCreateColorArraysThread, WWorkerThread< Function_T >, and WThreadedRunnerImpl.

Definition at line 126 of file WThreadedRunner.cpp.

References WLogger::addLogMessage(), and WLogger::getLogger().

Referenced by threadMainSave().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ threadMainSave()

void WThreadedRunner::threadMainSave ( )
private

The is the thread entry point.

It does exception handling and calls threadMain.

Definition at line 108 of file WThreadedRunner.cpp.

References getThreadName(), onThreadException(), setThisThreadName(), and threadMain().

Referenced by run().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ wait()

void WThreadedRunner::wait ( bool  requestFinish = false)

Wait for the thread to be finished.

Parameters
requestFinishtrue if the thread should be notified.

Definition at line 85 of file WThreadedRunner.cpp.

References m_thread, and requestStop().

Referenced by WDataSetFibers::init(), WQtGui::run(), WScriptUI::run(), WWorkerThreadTest::testExceptions(), WWorkerThreadTest::testMultipleThreads(), WThreadedRunnerTest::testRun(), WWorkerThreadTest::testSingleThread(), WWorkerThreadTest::testStopThread(), and WKdTree::WKdTree().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ waitForStop()

void WThreadedRunner::waitForStop ( )
protected

Let the thread sleep until a stop request was given.

Definition at line 103 of file WThreadedRunner.cpp.

Referenced by WMDistanceMapIsosurface::moduleMain(), WMTemplateContainers::moduleMain(), and WKernel::threadMain().

+ Here is the caller graph for this function:

◆ yield()

void WThreadedRunner::yield ( ) const
protected

Give remaining execution timeslice to another thread.

Definition at line 135 of file WThreadedRunner.cpp.

References m_thread.

Member Data Documentation

◆ m_crashMessage

std::string WThreadedRunner::m_crashMessage
protected

The crash message.

Only filled if m_isCrashed is true.

Definition at line 219 of file WThreadedRunner.h.

Referenced by getCrashMessage(), and handleDeadlyException().

◆ m_isCrashed

WBoolFlag WThreadedRunner::m_isCrashed
protected

True whenever an exception is thrown during threadMain.

Definition at line 214 of file WThreadedRunner.h.

Referenced by handleDeadlyException(), isCrashed(), WModuleContainer::moduleError(), and WModule::WModule().

◆ m_shutdownFlag

WBoolFlag WThreadedRunner::m_shutdownFlag
protected

Condition getting fired whenever the thread should quit.

This is useful for waiting for stop requests.

Definition at line 187 of file WThreadedRunner.h.

Referenced by WKernel::isFinishRequested(), WIGTLinkRemote::listenLoop(), WModuleImpl::moduleMain(), WMAnisotropicFiltering::moduleMain(), WMApplyMask::moduleMain(), WMArbitraryPlane::moduleMain(), WMArbitraryROIs::moduleMain(), WMButterfly::moduleMain(), WMCalculateGFA::moduleMain(), WMCalculateHistogram::moduleMain(), WMCalculateTensors::moduleMain(), WMClusterDisplay::moduleMain(), WMClusterDisplayVoxels::moduleMain(), WMColormapper::moduleMain(), WMCoordinateSystem::moduleMain(), WMData::moduleMain(), WMReadSphericalHarmonics::moduleMain(), WMWriteNIfTI::moduleMain(), WMDataCreatorFibers::moduleMain(), WMDataCreatorPoints::moduleMain(), WMDataCreatorScalar::moduleMain(), WMDataCreatorVector::moduleMain(), WMDatasetManipulator::moduleMain(), WMDatasetProfile::moduleMain(), WMDataTypeConversion::moduleMain(), WMDeterministicFTMori::moduleMain(), WMDiffTensorScalars::moduleMain(), WMDirectVolumeRendering::moduleMain(), WMDistanceMap::moduleMain(), WMEEGView::moduleMain(), WMEffectiveConnectivityCluster::moduleMain(), WMEigenSystem::moduleMain(), WMFiberDisplay::moduleMain(), WMFiberFilterIndex::moduleMain(), WMFiberFilterROI::moduleMain(), WMFiberParameterColoring::moduleMain(), WMFiberResampling::moduleMain(), WMFiberSelection::moduleMain(), WMFibersToPoints::moduleMain(), WMFiberTransform::moduleMain(), WMFiberTranslator::moduleMain(), WMFilterProtonData::moduleMain(), WMFunctionalMRIViewer::moduleMain(), WMGaussFiltering::moduleMain(), WMGridRenderer::moduleMain(), WMHARDIToSphericalHarmonics::moduleMain(), WMHierchClustDisplay::moduleMain(), WMPartition2Mesh::moduleMain(), WMHistogramEqualization::moduleMain(), WMHistogramView::moduleMain(), WMMultiHistogramView::moduleMain(), WMImageExtractor::moduleMain(), WMImageSpaceLIC::moduleMain(), WMImageSpaceTensorLIC::moduleMain(), WMIsosurface::moduleMain(), WMIsosurfaceRaytracer::moduleMain(), WMMergeComponentsToVector::moduleMain(), WMMergePoints::moduleMain(), WMMeshToPoints::moduleMain(), WMNavigationSlices::moduleMain(), WMOpenIGTLinkSender::moduleMain(), WMPaintTexture::moduleMain(), WMPickingDVR::moduleMain(), WMPickingDVREvaluation::moduleMain(), WMPointConnector::moduleMain(), WMPointRenderer::moduleMain(), WMProbTractDisplay::moduleMain(), WMProjectionsAsContext::moduleMain(), WMReadAmiraMesh::moduleMain(), WMReadDipoles::moduleMain(), WMReadLAS::moduleMain(), WMReadMesh::moduleMain(), WMReadRawData::moduleMain(), WMReadSimpleTextLineData::moduleMain(), WMReadVCL::moduleMain(), WMReadVIM::moduleMain(), WMResampleRegular::moduleMain(), WMSampleOnFibers::moduleMain(), WMScalarOperator::moduleMain(), WMScalarSegmentation::moduleMain(), WMSpatialDerivative::moduleMain(), WMSplineSurface::moduleMain(), WMSuperquadricGlyphs::moduleMain(), WMSurfaceIllustrator::moduleMain(), WMSurfaceParameterAnimator::moduleMain(), WMTeemGlyphs::moduleMain(), WMTemplate::moduleMain(), WMTemplateColormapping::moduleMain(), WMTemplateDataLoader::moduleMain(), WMTemplateRenderPipelines::moduleMain(), WMTemplateShaders::moduleMain(), WMTemplateUI::moduleMain(), WMTransferFunction1D::moduleMain(), WMTransferFunctionColorBar::moduleMain(), WMTriangleMeshRenderer::moduleMain(), WMVectorAlign::moduleMain(), WMVectorNormalize::moduleMain(), WMVectorOperator::moduleMain(), WMVectorPlot::moduleMain(), WMVectorScale::moduleMain(), WMVoxelizer::moduleMain(), WMWebglSupport::moduleMain(), WMWriteAmiraMesh::moduleMain(), WMWriteDendrogram::moduleMain(), WMWriteField::moduleMain(), WMWriteMesh::moduleMain(), WMWriteCSV::moduleMain(), WMWriteRawData::moduleMain(), WMWriteTracts::moduleMain(), WMWriteTransferFunction::moduleMain(), WMEEGView::openCustomWidget(), WIGTLinkRemote::readDataLoop(), requestStop(), WThreadedRunnerImpl::threadMain(), and WModule::WModule().

◆ m_thread

boost::thread WThreadedRunner::m_thread
protected

Thread instance.

Definition at line 158 of file WThreadedRunner.h.

Referenced by run(), wait(), and yield().

◆ m_threadName

std::string WThreadedRunner::m_threadName
private

This threads name.

Definition at line 250 of file WThreadedRunner.h.

Referenced by getThreadName(), and setThreadName().

◆ signal_thread_error

t_ThreadErrorSignalType WThreadedRunner::signal_thread_error
private

Signal fired whenever a thread throws an exception/error.

Definition at line 240 of file WThreadedRunner.h.

Referenced by handleDeadlyException(), and subscribeSignal().


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