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
qtconcurrenttask.qdoc
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page qtconcurrenttask.html
6 \title Concurrent Task
7 \brief A configurable way to run a task in a separate thread.
8 \ingroup thread
9
10 QtConcurrent::task provides an alternative interface for running a
11 task in a separate thread. The return value of the function is made
12 available through the QFuture API.
13
14 If you want to just run a function in a separate thread without adjusting
15 any parameters, use QtConcurrent::run as that lets you write less code.
16 The QtConcurrent::task is designed for cases where you need to perform
17 extra configurations steps.
18
19 This function is a part of the \l {Qt Concurrent} framework.
20
21 \section1 Optimize includes
22
23 If you include the \c <QtConcurrent> header, the entire Qt Concurrent
24 module with the entire Qt Core module will be included, which may increase
25 compilation times and binary sizes. To use the
26 \l {QtConcurrent::task}{QtConcurrent::task()} function, you can include a
27 more specific header:
28
29 \code
30 #include <QtConcurrentTask>
31 \endcode
32
33 \section1 Fluent interface
34
35 The QtConcurrent::task returns an instance of an auxiliary class called
36 QtConcurrent::QTaskBuilder. Normally, you don't need to create an instance
37 of this class manually. The QtConcurrent::QTaskBuilder provides an interface
38 to adjust different task parameters in a chain-like manner. This approach
39 is known as a
40 \l {https://en.wikipedia.org/wiki/Fluent_interface}{fluent interface}.
41
42 You can just set the parameters you need and then kick a task off.
43 In order to finalize the configuration of a task you must invoke
44 QtConcurrent::QTaskBuilder::spawn. This function is non-blocking (i.e.
45 returns a future object immediately), but it's not guaranteed that the
46 task starts immediately. You can use the QFuture and QFutureWatcher classes
47 to monitor the status of the task.
48
49 See more examples and explanations below.
50
51 \section1 Running a task in a separate thread
52
53 To run a function in another thread, use QtConcurrent::QTaskBuilder::spawn:
54
55 \snippet code/src_concurrent_qtconcurrenttask.cpp 0
56
57 This will run a lambda function in a separate thread obtained from
58 the default QThreadPool.
59
60 \section1 Passing arguments to the task
61
62 Invoking a function with arguments is done by passing them to
63 QtConcurrent::QTaskBuilder::withArguments:
64
65 \snippet code/src_concurrent_qtconcurrenttask.cpp 1
66
67 A copy of each argument is made at the point where
68 QtConcurrent::QTaskBuilder::withArguments is called, and these values
69 are passed to the thread when it begins executing the task. Changes made
70 to the arguments after calling QtConcurrent::QTaskBuilder::withArguments
71 are not visible to the thread.
72
73 If you want to run a function that accepts arguments by reference, you
74 should use \l {https://en.cppreference.com/w/cpp/utility/functional/ref}
75 {std::ref/cref} auxiliary functions. These functions create thin wrappers
76 around passed arguments:
77
78 \snippet code/src_concurrent_qtconcurrenttask.cpp 2
79
80 Make sure that all wrapped objects live long enough. It is possible to
81 get undefined behavior if a task outlives the object wrapped by
82 std::ref/cref.
83
84 \section1 Returning values from the task
85
86 You can obtain the result of a task with the QFuture API:
87
88 \snippet code/src_concurrent_qtconcurrenttask.cpp 3
89
90 Note that QFuture::result() is a blocking call, it waits for the
91 result to become available. Use QFutureWatcher to get a notification
92 when the task has finished execution and the result is available.
93
94 In case you want to pass a result to another asynchronous task, you can
95 use QFuture::then() to create a chain of dependent tasks. See the QFuture
96 documentation for more details.
97
98 \section1 Additional API features
99
100 \section2 Using different types of callable objects
101
102 Strictly speaking, you can use any type of tasks and arguments that
103 satisfy the following condition:
104
105 \snippet code/src_concurrent_qtconcurrenttask.cpp 4
106
107 You can use a free function:
108
109 \snippet code/src_concurrent_qtconcurrenttask.cpp 5
110
111 You can use a member function:
112
113 \snippet code/src_concurrent_qtconcurrenttask.cpp 6
114
115 You can use a callable object with an operator():
116
117 \snippet code/src_concurrent_qtconcurrenttask.cpp 7
118
119 If you want to use an existing callable object, you need to either
120 copy/move it to QtConcurrent::task or wrap it with std::ref/cref:
121
122 \snippet code/src_concurrent_qtconcurrenttask.cpp 8
123
124 \section2 Using custom thread pool
125
126 You can specify a custom thread pool:
127
128 \snippet code/src_concurrent_qtconcurrenttask.cpp 9
129
130 \section2 Setting priority for a task
131
132 You can set the priority for a task:
133
134 \snippet code/src_concurrent_qtconcurrenttask.cpp 10
135
136 If you don't need a future object, you can call
137 QtConcurrent::QTaskBuilder::spawn(QtConcurrent::FutureResult::Ignore):
138
139 \snippet code/src_concurrent_qtconcurrenttask.cpp 11
140
141 You can access the promise object associated with the task by defining an
142 additional argument of \c {QPromise<T> &} type inside the function.
143 This additional argument must be the first argument passed to the function, and
144 like in \l {Concurrent Run With Promise} mode, the function is expected to return void type.
145 Result reporting is done through QPromise API:
146
147 \snippet code/src_concurrent_qtconcurrenttask.cpp 12
148*/
149
150/*!
151 \fn template <typename Task> [[nodiscard]] QTaskBuilder<Task> QtConcurrent::task(Task &&task);
152 \since 6.0
153
154 Creates an instance of QtConcurrent::QTaskBuilder. This object can be used
155 to adjust some parameters and run \a task in a separate thread.
156
157 \sa {Concurrent Task}, QtConcurrent::QTaskBuilder
158*/