swift
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
swift::misc::CContinuousWorker Class Reference

Base class for a long-lived worker object which lives in its own thread. More...

Inheritance diagram for swift::misc::CContinuousWorker:
[legend]

Public Member Functions

 CContinuousWorker (QObject *owner, const QString &name)
 Constructor. More...
 
void start (QThread::Priority priority=QThread::InheritPriority)
 Starts a thread and moves the worker into it.
 
void quit () noexcept final
 Stops the thread the next time around its event loop. The thread and the worker will then be deleted. More...
 
void quitAndWait () noexcept final
 Calls quit() and blocks until the thread is finished. More...
 
bool isEnabled () const
 Enabled (running)? More...
 
void setEnabled (bool enabled)
 Enabled (running)? More...
 
void startUpdating (int updateTimeSecs)
 Start updating (start/stop timer) More...
 
const QString & getName ()
 Name of the worker.
 
const QObject * owner () const
 Owner of the worker.
 
- Public Member Functions inherited from swift::misc::CWorkerBase
template<typename T , typename F >
void then (T *context, F functor)
 Connects to a functor or method which will be called when the task is finished. More...
 
template<typename F >
void then (F functor)
 Connects to a functor which will be called when the task is finished. More...
 
bool isFinished () const
 Returns true if the task has finished. More...
 
template<typename F >
void doIfFinished (F functor) const
 Executes some code (in the caller's thread) if the task has finished. More...
 
template<typename F >
void doIfNotFinished (F functor) const
 Executes some code (in the caller's thread) if the task has not finished. More...
 
template<typename F1 , typename F2 >
void doIfFinishedElse (F1 ifFunctor, F2 elseFunctor) const
 Executes some code (in the caller's thread) if the task has finished and some different code if it has not finished. More...
 
void waitForFinished () noexcept
 Blocks until the task is finished. More...
 
void abandon () noexcept
 Notify the task that its result is no longer needed, so it can finish early. More...
 
void abandonAndWait () noexcept
 Convenience to call abandon() followed by waitForFinished().
 

Protected Member Functions

virtual void initialize ()
 Called when the thread is started.
 
virtual void cleanup ()
 Called when the thread is finished.
 
virtual void beforeQuit () noexcept
 Called before quit is called. More...
 
virtual unsigned long waitTimeoutMs () const
 Wait time for quitAndWait, 0 means not waiting.
 
void stopUpdateTimer ()
 Safely stop update time.
 
- Protected Member Functions inherited from swift::misc::CWorkerBase
 CWorkerBase ()
 Constructor.
 
 ~CWorkerBase ()
 Destructor.
 
bool isAbandoned () const
 For the task to check whether it can finish early. More...
 
bool hasStarted () const
 True if the worker has started.
 
void setStarted ()
 Mark the task as started.
 
void setFinished ()
 Mark the task as finished.
 

Protected Attributes

QTimer m_updateTimer { this }
 timer which can be used by implementing classes
 

Additional Inherited Members

- Signals inherited from swift::misc::CWorkerBase
void aboutToStart ()
 Emitted when the task is about to start.
 
void finished ()
 Emitted when the task is finished. More...
 
- Static Public Member Functions inherited from swift::misc::CWorkerBase
static const QStringList & getLogCategories ()
 Log categories.
 
static const QSet< CWorkerBase * > & allWorkers ()
 All workers currently existing.
 

Detailed Description

Base class for a long-lived worker object which lives in its own thread.

Definition at line 274 of file worker.h.

Constructor & Destructor Documentation

◆ CContinuousWorker()

swift::misc::CContinuousWorker::CContinuousWorker ( QObject *  owner,
const QString &  name 
)

Constructor.

Parameters
ownerWill be the parent of the new thread (the worker has no parent).
nameA name for the worker, which will be used to create a name for the thread.

Definition at line 159 of file worker.cpp.

Member Function Documentation

◆ beforeQuit()

virtual void swift::misc::CContinuousWorker::beforeQuit ( )
inlineprotectedvirtualnoexcept

Called before quit is called.

Remarks
can be used to "clean things up" or request work functions to stop

Reimplemented in swift::sound::CThreadedTonePairPlayer, and swift::misc::simulation::CBackgroundValidation.

Definition at line 325 of file worker.h.

◆ isEnabled()

bool swift::misc::CContinuousWorker::isEnabled ( ) const
inline

Enabled (running)?

Remarks
This function is thread safe.

Definition at line 300 of file worker.h.

◆ quit()

void swift::misc::CContinuousWorker::quit ( )
finalvirtualnoexcept

Stops the thread the next time around its event loop. The thread and the worker will then be deleted.

Remarks
This function is thread safe.

Reimplemented from swift::misc::CWorkerBase.

Definition at line 193 of file worker.cpp.

◆ quitAndWait()

void swift::misc::CContinuousWorker::quitAndWait ( )
finalvirtualnoexcept

Calls quit() and blocks until the thread is finished.

Remarks
This function is thread safe. Will deadlock if called by the worker thread.
Todo:
KB 2017-10 temp workaround: in T145 this will be fixed, sometimes (very rarely) hanging here during shutdown

Reimplemented from swift::misc::CWorkerBase.

Definition at line 205 of file worker.cpp.

◆ setEnabled()

void swift::misc::CContinuousWorker::setEnabled ( bool  enabled)
inline

Enabled (running)?

Remarks
This function is thread safe.

Definition at line 304 of file worker.h.

◆ startUpdating()

void swift::misc::CContinuousWorker::startUpdating ( int  updateTimeSecs)

Start updating (start/stop timer)

Remarks
This function is thread safe.

Definition at line 235 of file worker.cpp.


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