Workspace 7.0.2
|
Perform a task in a specific thread and wait for the result. More...
#include <Workspace/Application/System/blockingtask.h>
Signals | |
void | performTask (bool *result) |
Public Member Functions | |
~BlockingTask () override | |
bool | doTask () |
Static Public Member Functions | |
static bool | doTask (std::function< bool()> &&work, QThread *homeThread=QCoreApplication::instance() ->thread()) |
Protected Slots | |
virtual void | taskWrapper (bool *result) |
Protected Member Functions | |
BlockingTask (QThread *homeThread=QThread::currentThread()) | |
QSemaphore & | semaphore () |
virtual bool | task ()=0 |
This class ensures that a task is performed in the thread that owns the BlockingTask object (ownership can be conveniently set in the constructor). The call to doTask() blocks the calling thread until the task finishes. If the calling thread is the same as the BlockingTask thread, the task is performed in the calling thread and no thread blocking is required.
Use of this class can lead to deadlock if you cannot guarantee that the thread owning BlockingTask will not try to block on the calling thread when given the chance to run. Because the implementation of doTask() uses Qt's event loop processing to get the task to be performed in the appropriate thread, other events may be processed before the event that runs the task. If those other events try to make a blocking call to the thread that calls doTask(), deadlock will occur.
|
protected |
homeThread | The thread that should run the task. If this parameter is omitted, the current thread will be the owner (this is consistent with the default behavior of how QObject's are created by Qt). |
|
override |
The destructor should never be invoked while there is a running task.
bool doTask | ( | ) |
Directly or indirectly calls task() in such a way that ensures the call is only ever made in the thread that owns this BlockingTask object. Clients should call this function when they want the task to be performed. The function will not return until the task has completed. Note that this may involve blocking the calling thread for an arbitrarily long time depending on how long the task takes.
|
static |
Helper function to make it easier using the BlockingTask
A helper class to wrap a callable as a BlockingTask.
|
signal |
|
protected |
Return the reference to the semaphore for sub-class access.
|
protectedpure virtual |
Subclasses must implement this function to perform the work of the task. It can be assumed that task() is only ever called in the thread that owns the BlockingTask object, which is the whole reason for the existence of the BlockingTask class.
Implemented in ThreadedChartRender.
|
protectedvirtualslot |