Workspace 7.0.2
Signals | Public Member Functions | Static Public Member Functions | Protected Slots | Protected Member Functions | List of all members
BlockingTask Class Referenceabstract

Perform a task in a specific thread and wait for the result. More...

#include <Workspace/Application/System/blockingtask.h>

Inheritance diagram for BlockingTask:
[legend]

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
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ BlockingTask()

BlockingTask ( QThread *  homeThread = QThread::currentThread())
protected
Parameters
homeThreadThe 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).

◆ ~BlockingTask()

~BlockingTask ( )
override

The destructor should never be invoked while there is a running task.

Member Function Documentation

◆ doTask() [1/2]

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.

Returns
The value returned by the call to task().

◆ doTask() [2/2]

bool doTask ( std::function< bool()> &&  work,
QThread *  homeThread = QCoreApplication::instance()->thread() 
)
static

Helper function to make it easier using the BlockingTask

A helper class to wrap a callable as a BlockingTask.

◆ performTask

void performTask ( bool *  result)
signal

◆ semaphore()

QSemaphore & semaphore ( )
protected

Return the reference to the semaphore for sub-class access.

◆ task()

virtual bool task ( )
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.

Returns
True if the task was successful, or false otherwise. This value will be returned by doTask().

Implemented in ThreadedChartRender.

◆ taskWrapper

void taskWrapper ( bool *  result)
protectedvirtualslot