Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QRunnable Class Referenceabstract

\inmodule QtCore More...

#include <qrunnable.h>

+ Inheritance diagram for QRunnable:
+ Collaboration diagram for QRunnable:

Public Types

template<typename Callable >
using if_callable = std::enable_if_t<std::is_invocable_r_v<void, Callable>, bool>
 

Public Member Functions

virtual void run ()=0
 Implement this pure virtual function in your subclass.
 
constexpr QRunnable () noexcept=default
 Constructs a QRunnable.
 
virtual ~QRunnable ()
 QRunnable virtual destructor.
 
bool autoDelete () const
 Returns true is auto-deletion is enabled; false otherwise.
 
void setAutoDelete (bool autoDelete)
 Enables auto-deletion if autoDelete is true; otherwise auto-deletion is disabled.
 
template<typename Callable , if_callable< Callable > = true>
 QGenericRunnable (Callable &&c)
 
 ~QGenericRunnable () override
 
void run () override
 

Static Public Member Functions

template<typename Callable , if_callable< Callable > = true>
static QRunnablecreate (Callable &&functionToRun)
 
static QRunnablecreate (std::nullptr_t)=delete
 

Detailed Description

\inmodule QtCore

Since
4.4

The QRunnable class is the base class for all runnable objects.

The QRunnable class is an interface for representing a task or piece of code that needs to be executed, represented by your reimplementation of the run() function.

You can use QThreadPool to execute your code in a separate thread. QThreadPool deletes the QRunnable automatically if autoDelete() returns true (the default). Use setAutoDelete() to change the auto-deletion flag.

QThreadPool supports executing the same QRunnable more than once by calling QThreadPool::tryStart(this) from within the run() function. If autoDelete is enabled the QRunnable will be deleted when the last thread exits the run function. Calling QThreadPool::start() multiple times with the same QRunnable when autoDelete is enabled creates a race condition and is not recommended.

See also
QThreadPool

Definition at line 17 of file qrunnable.h.

Member Typedef Documentation

◆ if_callable

template<typename Callable >
using QRunnable::if_callable = std::enable_if_t<std::is_invocable_r_v<void, Callable>, bool>

Definition at line 31 of file qrunnable.h.

Constructor & Destructor Documentation

◆ QRunnable()

QRunnable::QRunnable ( )
constexprdefaultnoexcept

Constructs a QRunnable.

Auto-deletion is enabled by default.

See also
autoDelete(), setAutoDelete()

◆ ~QRunnable()

QRunnable::~QRunnable ( )
virtual

QRunnable virtual destructor.

Definition at line 10 of file qrunnable.cpp.

◆ ~QGenericRunnable()

QRunnable::~QGenericRunnable ( )
override

Member Function Documentation

◆ autoDelete()

bool QRunnable::autoDelete ( ) const
inline

Returns true is auto-deletion is enabled; false otherwise.

If auto-deletion is enabled, QThreadPool will automatically delete this runnable after calling run(); otherwise, ownership remains with the application programmer.

See also
setAutoDelete(), QThreadPool

Definition at line 37 of file qrunnable.h.

Referenced by QThreadPoolThread::run(), and QThreadPoolPrivate::stealAndRunRunnable().

+ Here is the caller graph for this function:

◆ create() [1/2]

template<typename Callable , QRunnable::if_callable< Callable > >
QRunnable * QRunnable::create ( Callable && functionToRun)
static

Definition at line 114 of file qrunnable.h.

References QGenericRunnable().

Referenced by QThreadPool::start(), QThreadPool::startOnReservedThread(), and QThreadPool::tryStart().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ create() [2/2]

static QRunnable * QRunnable::create ( std::nullptr_t )
staticdelete

◆ QGenericRunnable()

template<typename Callable , if_callable< Callable > = true>
QRunnable::QGenericRunnable ( Callable && c)
inlineexplicit

Definition at line 90 of file qrunnable.h.

Referenced by create().

+ Here is the caller graph for this function:

◆ run() [1/2]

void QRunnable::run ( )
override

◆ run() [2/2]

◆ setAutoDelete()

bool QRunnable::setAutoDelete ( bool autoDelete)
inline

Enables auto-deletion if autoDelete is true; otherwise auto-deletion is disabled.

If auto-deletion is enabled, QThreadPool will automatically delete this runnable after calling run(); otherwise, ownership remains with the application programmer.

Note that this flag must be set before calling QThreadPool::start(). Calling this function after QThreadPool::start() results in undefined behavior.

See also
autoDelete(), QThreadPool

Definition at line 38 of file qrunnable.h.

Referenced by QHostInfoRunnable::QHostInfoRunnable(), QQmlXmlListModelQueryRunnable::QQmlXmlListModelQueryRunnable(), QtConcurrent::ThreadEngineBase::ThreadEngineBase(), and QQuickShapeGenericRenderer::endSync().

+ Here is the caller graph for this function:

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