Base class for all classes needing to be executed in a separate thread. More...
#include <WThreadedRunner.h>
Public Types | |
typedef boost::function< void(void) > | THREADFUNCTION |
Type used for simple thread functions. | |
Public Member Functions | |
WThreadedRunner () | |
Default constructor. | |
virtual | ~WThreadedRunner () |
Destructor. | |
virtual void | run () |
Run thread. | |
void | run (THREADFUNCTION f) |
Run thread. | |
void | wait (bool requestFinish=false) |
Wait for the thread to be finished. | |
virtual void | requestStop () |
This method's purpose is to request a stop without waiting for it. | |
Protected Member Functions | |
virtual void | threadMain () |
Function that has to be overwritten for execution. | |
virtual void | notifyStop () |
Gets called when the thread should be stopped. | |
void | yield () const |
Give remaining execution timeslice to another thread. | |
void | sleep (const int32_t t) const |
Sets thread asleep. | |
void | msleep (const int32_t t) const |
Sets thread asleep. | |
void | waitForStop () |
Let the thread sleep until a stop request was given. | |
Protected Attributes | |
boost::thread | m_thread |
Thread instance. | |
WBoolFlag | m_shutdownFlag |
Condition getting fired whenever the thread should quit. | |
Private Member Functions | |
WThreadedRunner (const WThreadedRunner &rhs) | |
Disallow copy construction. | |
WThreadedRunner & | operator= (const WThreadedRunner &rhs) |
Disallow copy assignment. |
Base class for all classes needing to be executed in a separate thread.
Definition at line 41 of file WThreadedRunner.h.
typedef boost::function< void ( void ) > WThreadedRunner::THREADFUNCTION |
Type used for simple thread functions.
Definition at line 48 of file WThreadedRunner.h.
WThreadedRunner::WThreadedRunner | ( | ) |
Default constructor.
Definition at line 33 of file WThreadedRunner.cpp.
WThreadedRunner::~WThreadedRunner | ( | ) | [virtual] |
Destructor.
Definition at line 39 of file WThreadedRunner.cpp.
WThreadedRunner::WThreadedRunner | ( | const WThreadedRunner & | rhs | ) | [private] |
Disallow copy construction.
rhs | the other threaded runner. |
void WThreadedRunner::msleep | ( | const int32_t | t | ) | const [protected] |
Sets thread asleep.
t | time to sleep in microseconds. |
Definition at line 98 of file WThreadedRunner.cpp.
References sleep().
void WThreadedRunner::notifyStop | ( | ) | [protected, virtual] |
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 84 of file WThreadedRunner.cpp.
Referenced by requestStop().
WThreadedRunner& WThreadedRunner::operator= | ( | const WThreadedRunner & | rhs | ) | [private] |
Disallow copy assignment.
rhs | the other threaded runner. |
void WThreadedRunner::requestStop | ( | ) | [virtual] |
This method's purpose is to request a stop without waiting for it.
Definition at line 65 of file WThreadedRunner.cpp.
References m_shutdownFlag, and notifyStop().
Referenced by WModuleContainer::moduleError(), WWorkerThreadTest::testStopThread(), and wait().
void WThreadedRunner::run | ( | ) | [virtual] |
Run thread.
Definition at line 46 of file WThreadedRunner.cpp.
References threadMain().
Referenced by WDataSetFibers::init(), WProjectFile::load(), 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 51 of file WThreadedRunner.cpp.
References m_thread.
void WThreadedRunner::sleep | ( | const int32_t | t | ) | const [protected] |
Sets thread asleep.
t | time to sleep in seconds. |
Definition at line 93 of file WThreadedRunner.cpp.
Referenced by WModuleImpl::moduleMain(), msleep(), WThreadedRunnerTest::testSleep(), and WThreadedRunnerImpl::threadMain().
void WThreadedRunner::threadMain | ( | ) | [protected, virtual] |
Function that has to be overwritten for execution.
It gets executed in a separate thread after run() has been called.
Reimplemented in WThreadedRunnerImpl, WWorkerThread< Function_T >, WCreateColorArraysThread, WGraphicsEngine, WKdTreeThread, and WKernel.
Definition at line 79 of file WThreadedRunner.cpp.
References WLogger::addLogMessage(), and WLogger::getLogger().
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 56 of file WThreadedRunner.cpp.
References m_thread, and requestStop().
Referenced by WDataSetFibers::init(), WWorkerThreadTest::testExceptions(), WWorkerThreadTest::testMultipleThreads(), WThreadedRunnerTest::testRun(), WWorkerThreadTest::testSingleThread(), WWorkerThreadTest::testStopThread(), and WKdTree::WKdTree().
void WThreadedRunner::waitForStop | ( | ) | [protected] |
Let the thread sleep until a stop request was given.
Definition at line 74 of file WThreadedRunner.cpp.
References m_shutdownFlag, and WFlag< T >::wait().
Referenced by WKernel::threadMain().
void WThreadedRunner::yield | ( | ) | const [protected] |
Give remaining execution timeslice to another thread.
Definition at line 88 of file WThreadedRunner.cpp.
References m_thread.
WBoolFlag WThreadedRunner::m_shutdownFlag [protected] |
Condition getting fired whenever the thread should quit.
This is useful for waiting for stop requests.
Definition at line 129 of file WThreadedRunner.h.
Referenced by WKernel::isFinishRequested(), WModuleImpl::moduleMain(), requestStop(), WWorkerThread< Function_T >::threadMain(), WThreadedRunnerImpl::threadMain(), waitForStop(), and WModule::WModule().
boost::thread WThreadedRunner::m_thread [protected] |
Thread instance.
Definition at line 100 of file WThreadedRunner.h.