ImFusion SDK 4.3
StoppableThread Class Referencefinal

#include <ImFusion/Core/Threading/StoppableThread.h>

Extension of a std::thread to allow for signalling the thread to abort its execution. More...

+ Inheritance diagram for StoppableThread:

Detailed Description

Extension of a std::thread to allow for signalling the thread to abort its execution.

This class adds functionality for cooperative abortion of a running thread similar to the interface of boost::thread and the upcoming std::jthread: Other threads can signal the StoppableThread that they request abortion by calling requestStop(). In order for this to work the StoppableThread must regularly check during its execution, whether abortion was requested using isStopRequested(), and if so stop its execution.

Example:

StoppableThread stoppableThread([]() {
while (true) { // continuously running loop performing computations
[...]
// periodically check whether abortion was requested
break;
}
});
// signal the thread to stop
stoppableThread.requestStop();
// should join in finite time
stoppableThread.join();
StoppableThread(Function &&f, Args &&... args)
Instantiate a new interruptible thread that will execute the given function.
Definition StoppableThread.h:109
static bool isStopRequested()
Check if abortion was requested for the current thread.
Note
For lifetime reasons, a StoppableThread can not be detached and will always implicitly join on destruction.

Classes

struct  StopException
 Special exception type that is thrown by throwIfStopRequested() if abortion was requested. More...
 

Public Member Functions

template<typename Function, typename... Args>
 StoppableThread (Function &&f, Args &&... args)
 Instantiate a new interruptible thread that will execute the given function.
 
 ~StoppableThread ()
 If the thread is joinable, will call requestStop() and then join() the thread.
 
 StoppableThread (StoppableThread &&)=delete
 StoppableThread is not movable because we can't notify the running thread that his stopSignal ref has changed.
 
StoppableThreadoperator= (StoppableThread &&)=delete
 
void requestStop (bool stop=true)
 Signal to the thread that it should abort its execution.
 
void join ()
 Wait for the thread to finish its execution.
 
bool isRunning ()
 Check whether the thread function is still running or not.
 
std::thread::id threadId () const
 Return the internal thread id.
 

Static Public Member Functions

static bool isStopRequested ()
 Check if abortion was requested for the current thread.
 
static void throwIfStopRequested ()
 Use exceptions to check if abortion was requested for the current thread.
 

Additional Inherited Members

- Protected Member Functions inherited from NotCopyable
 NotCopyable (NotCopyable &&) noexcept=default
 
NotCopyableoperator= (NotCopyable &&) noexcept=default
 
 NotCopyable (const NotCopyable &)=delete
 
NotCopyableoperator= (const NotCopyable &)=delete
 

Constructor & Destructor Documentation

◆ StoppableThread()

template<typename Function, typename... Args>
StoppableThread ( Function && f,
Args &&... args )

Instantiate a new interruptible thread that will execute the given function.

Parameters
fFunction to execute in the new thread.
argsArguments to pass to f.

Member Function Documentation

◆ throwIfStopRequested()

static void throwIfStopRequested ( )
static

Use exceptions to check if abortion was requested for the current thread.

Exceptions
StopExceptionif abortion was requested for the current thread.

◆ requestStop()

void requestStop ( bool stop = true)

Signal to the thread that it should abort its execution.

The thread needs to check regularly for this signal in order for this to work. If you set stop to false you can reset the abortion flag in case future computations should no longer be aborted.

◆ join()

void join ( )

Wait for the thread to finish its execution.

Calling join() on a non-joinable thread will have no effect.

Note
Calling this function will not request the thread to stop.

The documentation for this class was generated from the following file:
Search Tab / S to search, Esc to close