Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qrunnable.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qrunnable.h"
6
7#include <QtCore/qlogging.h>
8
10
11QRunnable::~QRunnable()
12{
13}
14
15/*!
16 \internal
17 Prints a warning and returns nullptr.
18*/
19QRunnable *QRunnable::warnNullCallable()
20{
21 qWarning("Trying to create null QRunnable. This may stop working.");
22 return nullptr;
23}
24
25
26QRunnable::QGenericRunnable::~QGenericRunnable()
27{
28 runHelper->destroy();
29}
30
31void QRunnable::QGenericRunnable::run()
32{
33 runHelper->run();
34}
35
36/*!
37 \class QRunnable
38 \inmodule QtCore
39 \since 4.4
40 \brief The QRunnable class is the base class for all runnable objects.
41
42 \ingroup thread
43
44 The QRunnable class is an interface for representing a task or
45 piece of code that needs to be executed, represented by your
46 reimplementation of the run() function.
47
48 You can use QThreadPool to execute your code in a separate
49 thread. QThreadPool deletes the QRunnable automatically if
50 autoDelete() returns \c true (the default). Use setAutoDelete() to
51 change the auto-deletion flag.
52
53 QThreadPool supports executing the same QRunnable more than once
54 by calling QThreadPool::tryStart(this) from within the run() function.
55 If autoDelete is enabled the QRunnable will be deleted when
56 the last thread exits the run function. Calling QThreadPool::start()
57 multiple times with the same QRunnable when autoDelete is enabled
58 creates a race condition and is not recommended.
59
60 \sa QThreadPool
61*/
62
63/*! \fn QRunnable::run()
64 Implement this pure virtual function in your subclass.
65*/
66
67/*! \fn QRunnable::QRunnable()
68 Constructs a QRunnable. Auto-deletion is enabled by default.
69
70 \sa autoDelete(), setAutoDelete()
71*/
72
73/*! \fn QRunnable::~QRunnable()
74 QRunnable virtual destructor.
75*/
76
77/*! \fn bool QRunnable::autoDelete() const
78
79 Returns \c true is auto-deletion is enabled; false otherwise.
80
81 If auto-deletion is enabled, QThreadPool will automatically delete
82 this runnable after calling run(); otherwise, ownership remains
83 with the application programmer.
84
85 \sa setAutoDelete(), QThreadPool
86*/
87
88/*! \fn bool QRunnable::setAutoDelete(bool autoDelete)
89
90 Enables auto-deletion if \a autoDelete is true; otherwise
91 auto-deletion is disabled.
92
93 If auto-deletion is enabled, QThreadPool will automatically delete
94 this runnable after calling run(); otherwise, ownership remains
95 with the application programmer.
96
97 Note that this flag must be set before calling
98 QThreadPool::start(). Calling this function after
99 QThreadPool::start() results in undefined behavior.
100
101 \sa autoDelete(), QThreadPool
102*/
103
104/*!
105 \fn template<typename Callable, QRunnable::if_callable<Callable>> QRunnable *QRunnable::create(Callable &&callableToRun);
106 \since 5.15
107
108 Creates a QRunnable that calls \a callableToRun in run().
109
110 Auto-deletion is enabled by default.
111
112 \note In Qt versions prior to 6.6, this method took copyable functions only.
113
114 \constraints \c Callable
115 is a function or function object which can be called with zero arguments.
116
117 \sa run(), autoDelete()
118*/
119
120QT_END_NAMESPACE