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