![]() |
ImFusion SDK 4.3
|
#include <ImFusion/Core/Threading/StoppableThread.h>
Extension of a std::thread to allow for signalling the thread to abort its execution. More...
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:
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. | |
StoppableThread & | operator= (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 | |
![]() | |
NotCopyable (NotCopyable &&) noexcept=default | |
NotCopyable & | operator= (NotCopyable &&) noexcept=default |
NotCopyable (const NotCopyable &)=delete | |
NotCopyable & | operator= (const NotCopyable &)=delete |
StoppableThread | ( | Function && | f, |
Args &&... | args ) |
Instantiate a new interruptible thread that will execute the given function.
f | Function to execute in the new thread. |
args | Arguments to pass to f. |
|
static |
Use exceptions to check if abortion was requested for the current thread.
StopException | if abortion was requested for the current thread. |
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.
void join | ( | ) |
Wait for the thread to finish its execution.
Calling join() on a non-joinable thread will have no effect.