OpenWalnut
1.5.0dev
|
Base class for all classes needing to be executed in a separate thread. More...
#include <WThreadedRunner.h>
Public Types | |
typedef std::shared_ptr< WThreadedRunner > | SPtr |
Abbreviation to a shared_ptr to this type. More... | |
typedef std::shared_ptr< const WThreadedRunner > | ConstSPtr |
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 WBoolFlag & | isCrashed () 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... | |
WThreadedRunner & | operator= (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... | |
Base class for all classes needing to be executed in a separate thread.
Definition at line 43 of file WThreadedRunner.h.
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.
typedef std::shared_ptr< WThreadedRunner > WThreadedRunner::SPtr |
Abbreviation to a shared_ptr to this type.
Definition at line 49 of file WThreadedRunner.h.
typedef boost::function< void ( void ) > WThreadedRunner::THREADFUNCTION |
Type used for simple thread functions.
Definition at line 59 of file WThreadedRunner.h.
WThreadedRunner::WThreadedRunner | ( | ) |
Default constructor.
Definition at line 39 of file WThreadedRunner.cpp.
|
virtual |
Destructor.
Definition at line 48 of file WThreadedRunner.cpp.
|
private |
Disallow copy construction.
rhs | the other threaded runner. |
const std::string & WThreadedRunner::getCrashMessage | ( | ) | const |
Get the message of the exception finally causing the crash.
Definition at line 172 of file WThreadedRunner.cpp.
References m_crashMessage.
std::string WThreadedRunner::getThreadName | ( | ) | const |
Returns the current thread name.
Definition at line 167 of file WThreadedRunner.cpp.
References m_threadName.
Referenced by threadMainSave().
|
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.
e | the exception |
sender | allows 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().
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.
Definition at line 53 of file WThreadedRunner.cpp.
References m_isCrashed.
|
protected |
Sets thread asleep.
t | time to sleep in microseconds. |
Definition at line 145 of file WThreadedRunner.cpp.
|
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().
|
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.
e | the exception that was caught. |
Reimplemented in WProjectFile, and WModule.
Definition at line 58 of file WThreadedRunner.cpp.
References handleDeadlyException().
Referenced by threadMainSave().
|
private |
Disallow copy assignment.
rhs | the other threaded runner. |
|
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().
|
virtual |
Run thread.
Reimplemented in WModuleCombiner, and WBatchLoader.
Definition at line 75 of file WThreadedRunner.cpp.
References threadMainSave().
Referenced by WModuleContainer::add(), WDataSetFibers::init(), WProjectFile::load(), WMHARDIToSphericalHarmonics::HARDICalculation::operator()(), WBatchLoader::run(), WModuleCombiner::run(), WWorkerThreadTest::testExceptions(), WWorkerThreadTest::testMultipleThreads(), WThreadedRunnerTest::testRun(), WWorkerThreadTest::testSingleThread(), WWorkerThreadTest::testStopThread(), and WKdTree::WKdTree().
void WThreadedRunner::run | ( | THREADFUNCTION | f | ) |
Run thread.
This does not start threadMain(() but runs a specified function instead.
f | the function to run instead of the threadMain method. |
Definition at line 80 of file WThreadedRunner.cpp.
References m_thread.
|
static |
Static function to set the name of the calling thread.
name | the name. |
Definition at line 184 of file WThreadedRunner.cpp.
Referenced by threadMainSave().
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.
name | the name |
Definition at line 162 of file WThreadedRunner.cpp.
References m_threadName.
Referenced by WModule::initialize(), WGraphicsEngine::WGraphicsEngine(), and WKernel::WKernel().
|
protected |
Sets thread asleep.
t | time 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().
|
virtual |
Connects a specified notify function with a signal this thread instance is offering.
WSignalSubscriptionFailed | thrown if the signal can't be connected. |
signal | the signal to connect to. |
notifier | the notifier function to bind. |
Definition at line 150 of file WThreadedRunner.cpp.
References signal_thread_error.
|
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().
|
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().
void WThreadedRunner::wait | ( | bool | requestFinish = false | ) |
Wait for the thread to be finished.
requestFinish | true 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().
|
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().
|
protected |
Give remaining execution timeslice to another thread.
Definition at line 135 of file WThreadedRunner.cpp.
References m_thread.
|
protected |
The crash message.
Only filled if m_isCrashed is true.
Definition at line 219 of file WThreadedRunner.h.
Referenced by getCrashMessage(), and handleDeadlyException().
|
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().
|
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().
|
protected |
Thread instance.
Definition at line 158 of file WThreadedRunner.h.
|
private |
This threads name.
Definition at line 250 of file WThreadedRunner.h.
Referenced by getThreadName(), and setThreadName().
|
private |
Signal fired whenever a thread throws an exception/error.
Definition at line 240 of file WThreadedRunner.h.
Referenced by handleDeadlyException(), and subscribeSignal().