1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtConcurrent module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40/*!
41 \page qtconcurrentrun.html
42 \title Concurrent Run
43 \ingroup thread
44
45 The QtConcurrent::run() function runs a function in a separate thread.
46 The return value of the function is made available through the QFuture API.
47
48 This function is a part of the \l {Qt Concurrent} framework.
49
50 \section1 Running a Function in a Separate Thread
51
52 To run a function in another thread, use QtConcurrent::run():
53
54 \snippet code/src_concurrent_qtconcurrentrun.cpp 0
55
56 This will run \e aFunction in a separate thread obtained from the default
57 QThreadPool. You can use the QFuture and QFutureWatcher classes to monitor
58 the status of the function.
59
60 To use a dedicated thread pool, you can pass the QThreadPool as
61 the first argument:
62
63 \snippet code/src_concurrent_qtconcurrentrun.cpp explicit-pool-0
64
65 \section1 Passing Arguments to the Function
66
67 Passing arguments to the function is done by adding them to the
68 QtConcurrent::run() call immediately after the function name. For example:
69
70 \snippet code/src_concurrent_qtconcurrentrun.cpp 1
71
72 A copy of each argument is made at the point where QtConcurrent::run() is
73 called, and these values are passed to the thread when it begins executing
74 the function. Changes made to the arguments after calling
75 QtConcurrent::run() are \e not visible to the thread.
76
77 \section1 Returning Values from the Function
78
79 Any return value from the function is available via QFuture:
80
81 \snippet code/src_concurrent_qtconcurrentrun.cpp 2
82
83 As documented above, passing arguments is done like this:
84
85 \snippet code/src_concurrent_qtconcurrentrun.cpp 3
86
87 Note that the QFuture::result() function blocks and waits for the result
88 to become available. Use QFutureWatcher to get notification when the
89 function has finished execution and the result is available.
90
91 \section1 Additional API Features
92
93 \section2 Using Member Functions
94
95 QtConcurrent::run() also accepts pointers to member functions. The first
96 argument must be either a const reference or a pointer to an instance of
97 the class. Passing by const reference is useful when calling const member
98 functions; passing by pointer is useful for calling non-const member
99 functions that modify the instance.
100
101 For example, calling QByteArray::split() (a const member function) in a
102 separate thread is done like this:
103
104 \snippet code/src_concurrent_qtconcurrentrun.cpp 4
105
106 Calling a non-const member function is done like this:
107
108 \snippet code/src_concurrent_qtconcurrentrun.cpp 5
109
110 \section2 Using Lambda Functions
111
112 Calling a lambda function is done like this:
113
114 \snippet code/src_concurrent_qtconcurrentrun.cpp 6
115*/
116
117/*!
118 \typedef Function
119 \internal
120
121 This typedef is a dummy required to make the \c Function
122 type name known so that clang doesn't reject functions
123 that use it.
124*/
125
126/*!
127 \fn QFuture<T> QtConcurrent::run(Function function, ...);
128
129 Equivalent to
130 \code
131 QtConcurrent::run(QThreadPool::globalInstance(), function, ...);
132 \endcode
133
134 Runs \a function in a separate thread. The thread is taken from the global
135 QThreadPool. Note that \a function may not run immediately; \a function
136 will only be run once a thread becomes available.
137
138 T is the same type as the return value of \a function. Non-void return
139 values can be accessed via the QFuture::result() function.
140
141 \note The QFuture returned can only be used to query for the
142 running/finished status and the return value of the function. In particular,
143 canceling or pausing can be issued only if the computations behind the future
144 has not been started.
145
146 \sa {Concurrent Run}
147*/
148
149/*!
150 \since 5.4
151 \fn QFuture<T> QtConcurrent::run(QThreadPool *pool, Function function, ...);
152
153 Runs \a function in a separate thread. The thread is taken from the
154 QThreadPool \a pool. Note that \a function may not run immediately; \a function
155 will only be run once a thread becomes available.
156
157 T is the same type as the return value of \a function. Non-void return
158 values can be accessed via the QFuture::result() function.
159
160 \note The QFuture returned can only be used to query for the
161 running/finished status and the return value of the function. In particular,
162 canceling or pausing can be issued only if the computations behind the future
163 has not been started.
164
165 \sa {Concurrent Run}
166*/
167