1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtCore module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
43 \namespace QtConcurrent
46 \brief The QtConcurrent namespace provides high-level APIs that make it
47 possible to write multi-threaded programs without using low-level
50 See the \l {Concurrent Programming}{Qt Concurrent} chapter in
51 the \l{threads.html}{threading} documentation.
58 \namespace QtConcurrent::internal
61 \brief The QtConcurrent::internal namespace contains QtConcurrent
62 implementation details.
66 \enum QtConcurrent::ReduceOption
67 This enum specifies the order of which results from the map or filter
68 function are passed to the reduce function.
70 \value UnorderedReduce Reduction is done in an arbitrary order.
71 \value OrderedReduce Reduction is done in the order of the
73 \value SequentialReduce Reduction is done sequentally: only one
74 thread will enter the reduce function at a time. (Parallel reduction
75 might be supported in a future version of Qt Concurrent.)
79 \headerfile <QtConcurrentMap>
80 \title Concurrent Map and Map-Reduce
83 \brief The <QtConcurrentMap> header provides concurrent Map and MapReduce.
85 These functions are a part of the \l {Concurrent Programming}{Qt Concurrent} framework.
87 The QtConcurrent::map(), QtConcurrent::mapped() and
88 QtConcurrent::mappedReduced() functions run computations in parallel on
89 the items in a sequence such as a QList or a QVector. QtConcurrent::map()
90 modifies a sequence in-place, QtConcurrent::mapped() returns a new
91 sequence containing the modified content, and QtConcurrent::mappedReduced()
92 returns a single result.
94 Each of the above functions has a blocking variant that returns
95 the final result instead of a QFuture. You use them in the same
96 way as the asynchronous variants.
98 \snippet doc/src/snippets/code/src_corelib_concurrent_qtconcurrentmap.cpp 7
100 Note that the result types above are not QFuture objects, but real result
101 types (in this case, QList<QImage> and QImage).
103 \section1 Concurrent Map
105 QtConcurrent::mapped() takes an input sequence and a map function. This map
106 function is then called for each item in the sequence, and a new sequence
107 containing the return values from the map function is returned.
109 The map function must be of the form:
111 \snippet doc/src/snippets/code/src_corelib_concurrent_qtconcurrentmap.cpp 0
113 T and U can be any type (and they can even be the same type), but T must
114 match the type stored in the sequence. The function returns the modified
115 or \e mapped content.
117 This example shows how to apply a scale function to all the items
120 \snippet doc/src/snippets/code/src_corelib_concurrent_qtconcurrentmap.cpp 1
122 The results of the map are made available through QFuture. See the
123 QFuture and QFutureWatcher documentation for more information on how to
124 use QFuture in your applications.
126 If you want to modify a sequence in-place, use QtConcurrent::map(). The
127 map function must then be of the form:
129 \snippet doc/src/snippets/code/src_corelib_concurrent_qtconcurrentmap.cpp 2
131 Note that the return value and return type of the map function are not
134 Using QtConcurrent::map() is similar to using QtConcurrent::mapped():
136 \snippet doc/src/snippets/code/src_corelib_concurrent_qtconcurrentmap.cpp 3
138 Since the sequence is modified in place, QtConcurrent::map() does not
139 return any results via QFuture. However, you can still use QFuture and
140 QFutureWatcher to monitor the status of the map.
142 \section1 Concurrent Map-Reduce
144 QtConcurrent::mappedReduced() is similar to QtConcurrent::mapped(), but
145 instead of returning a sequence with the new results, the results are
146 combined into a single value using a reduce function.
148 The reduce function must be of the form:
150 \snippet doc/src/snippets/code/src_corelib_concurrent_qtconcurrentmap.cpp 4
152 T is the type of the final result, U is the return type of the map
153 function. Note that the return value and return type of the reduce
154 function are not used.
156 Call QtConcurrent::mappedReduced() like this:
158 \snippet doc/src/snippets/code/src_corelib_concurrent_qtconcurrentmap.cpp 5
160 The reduce function will be called once for each result returned by the map
161 function, and should merge the \e{intermediate} into the \e{result}
162 variable. QtConcurrent::mappedReduced() guarantees that only one thread
163 will call reduce at a time, so using a mutex to lock the result variable
164 is not neccesary. The QtConcurrent::ReduceOptions enum provides a way to
165 control the order in which the reduction is done. If
166 QtConcurrent::UnorderedReduce is used (the default), the order is
167 undefined, while QtConcurrent::OrderedReduce ensures that the reduction
168 is done in the order of the original sequence.
170 \section1 Additional API Features
172 \section2 Using Iterators instead of Sequence
174 Each of the above functions has a variant that takes an iterator range
175 instead of a sequence. You use them in the same way as the sequence
178 \snippet doc/src/snippets/code/src_corelib_concurrent_qtconcurrentmap.cpp 6
180 \section2 Blocking Variants
182 Each of the above functions has a blocking variant that returns
183 the final result instead of a QFuture. You use them in the same
184 way as the asynchronous variants.
186 \snippet doc/src/snippets/code/src_corelib_concurrent_qtconcurrentmap.cpp 7
188 Note that the result types above are not QFuture objects, but real result
189 types (in this case, QList<QImage> and QImage).
191 \section2 Using Member Functions
193 QtConcurrent::map(), QtConcurrent::mapped(), and
194 QtConcurrent::mappedReduced() accept pointers to member functions.
195 The member function class type must match the type stored in the sequence:
197 \snippet doc/src/snippets/code/src_corelib_concurrent_qtconcurrentmap.cpp 8
199 Note that when using QtConcurrent::mappedReduced(), you can mix the use of
200 normal and member functions freely:
202 \snippet doc/src/snippets/code/src_corelib_concurrent_qtconcurrentmap.cpp 9
204 \section2 Using Function Objects
206 QtConcurrent::map(), QtConcurrent::mapped(), and
207 QtConcurrent::mappedReduced() accept function objects, which can be used to
208 add state to a function call. The result_type typedef must define the
209 result type of the function call operator:
211 \snippet doc/src/snippets/code/src_corelib_concurrent_qtconcurrentmap.cpp 14
213 \section2 Using Bound Function Arguments
215 Note that Qt does not provide support for bound functions. This is
216 provided by 3rd party libraries like
217 \l{http://www.boost.org/libs/bind/bind.html}{Boost} or
218 \l{http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf}{C++
219 TR1 Library Extensions}.
221 If you want to use a map function that takes more than one argument you can
222 use boost::bind() or std::tr1::bind() to transform it onto a function that
225 As an example, we'll use QImage::scaledToWidth():
227 \snippet doc/src/snippets/code/src_corelib_concurrent_qtconcurrentmap.cpp 10
229 scaledToWidth takes three arguments (including the "this" pointer) and
230 can't be used with QtConcurrent::mapped() directly, because
231 QtConcurrent::mapped() expects a function that takes one argument. To use
232 QImage::scaledToWidth() with QtConcurrent::mapped() we have to provide a
233 value for the \e{width} and the \e{transformation mode}:
235 \snippet doc/src/snippets/code/src_corelib_concurrent_qtconcurrentmap.cpp 11
237 The return value from boost::bind() is a function object (functor) with
238 the following signature:
240 \snippet doc/src/snippets/code/src_corelib_concurrent_qtconcurrentmap.cpp 12
242 This matches what QtConcurrent::mapped() expects, and the complete example
245 \snippet doc/src/snippets/code/src_corelib_concurrent_qtconcurrentmap.cpp 13
249 \fn QFuture<void> QtConcurrent::map(Sequence &sequence, MapFunction function)
250 \relates <QtConcurrentMap>
252 Calls \a function once for each item in \a sequence. The \a function is
253 passed a reference to the item, so that any modifications done to the item
254 will appear in \a sequence.
258 \fn QFuture<void> QtConcurrent::map(Iterator begin, Iterator end, MapFunction function)
259 \relates <QtConcurrentMap>
261 Calls \a function once for each item from \a begin to \a end. The
262 \a function is passed a reference to the item, so that any modifications
263 done to the item will appear in the sequence which the iterators belong to.
267 \fn QFuture<T> QtConcurrent::mapped(const Sequence &sequence, MapFunction function)
268 \relates <QtConcurrentMap>
270 Calls \a function once for each item in \a sequence and returns a future
271 with each mapped item as a result. You can use QFuture::const_iterator or
272 QFutureIterator to iterate through the results.
276 \fn QFuture<T> QtConcurrent::mapped(ConstIterator begin, ConstIterator end, MapFunction function)
277 \relates <QtConcurrentMap>
279 Calls \a function once for each item from \a begin to \a end and returns a
280 future with each mapped item as a result. You can use
281 QFuture::const_iterator or QFutureIterator to iterate through the results.
285 \fn QFuture<T> QtConcurrent::mappedReduced(const Sequence &sequence,
286 MapFunction mapFunction, ReduceFunction reduceFunction,
287 QtConcurrent::ReduceOptions reduceOptions)
289 \relates <QtConcurrentMap>
291 Calls \a mapFunction once for each item in \a sequence. The return value of
292 each \a mapFunction is passed to \a reduceFunction.
294 Note that while \a mapFunction is called concurrently, only one thread at a
295 time will call \a reduceFunction. The order in which \a reduceFunction is
296 called is determined by \a reduceOptions.
300 \fn QFuture<T> QtConcurrent::mappedReduced(ConstIterator begin,
301 ConstIterator end, MapFunction mapFunction, ReduceFunction reduceFunction,
302 QtConcurrent::ReduceOptions reduceOptions)
304 \relates <QtConcurrentMap>
306 Calls \a mapFunction once for each item from \a begin to \a end. The return
307 value of each \a mapFunction is passed to \a reduceFunction.
309 Note that while \a mapFunction is called concurrently, only one thread at a
310 time will call \a reduceFunction. By default, the order in which
311 \a reduceFunction is called is undefined.
313 \note QtConcurrent::OrderedReduce results in the ordered reduction.
317 \fn void QtConcurrent::blockingMap(Sequence &sequence, MapFunction function)
319 Calls \a function once for each item in \a sequence. The \a function is
320 passed a reference to the item, so that any modifications done to the item
321 will appear in \a sequence.
323 \note This function will block until all items in the sequence have been processed.
329 \fn void QtConcurrent::blockingMap(Iterator begin, Iterator end, MapFunction function)
331 Calls \a function once for each item from \a begin to \a end. The
332 \a function is passed a reference to the item, so that any modifications
333 done to the item will appear in the sequence which the iterators belong to.
335 \note This function will block until the iterator reaches the end of the
336 sequence being processed.
342 \fn T QtConcurrent::blockingMapped(const Sequence &sequence, MapFunction function)
344 Calls \a function once for each item in \a sequence and returns a Sequence containing
345 the results. The type of the results will match the type returned my the MapFunction.
347 \note This function will block until all items in the sequence have been processed.
353 \fn T QtConcurrent::blockingMapped(ConstIterator begin, ConstIterator end, MapFunction function)
355 Calls \a function once for each item from \a begin to \a end and returns a
356 container with the results. Specify the type of container as the a template
360 QList<int> ints = QtConcurrent::blockingMapped<QList<int> >(beginIterator, endIterator, fn);
363 \note This function will block until the iterator reaches the end of the
364 sequence being processed.
370 \fn T QtConcurrent::blockingMappedReduced(const Sequence &sequence, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions)
372 \relates <QtConcurrentMap>
374 Calls \a mapFunction once for each item in \a sequence. The return value of
375 each \a mapFunction is passed to \a reduceFunction.
377 Note that while \a mapFunction is called concurrently, only one thread at a
378 time will call \a reduceFunction. The order in which \a reduceFunction is
379 called is determined by \a reduceOptions.
381 \note This function will block until all items in the sequence have been processed.
387 \fn T QtConcurrent::blockingMappedReduced(ConstIterator begin, ConstIterator end, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions)
389 \relates <QtConcurrentMap>
391 Calls \a mapFunction once for each item from \a begin to \a end. The return
392 value of each \a mapFunction is passed to \a reduceFunction.
394 Note that while \a mapFunction is called concurrently, only one thread at a
395 time will call \a reduceFunction. The order in which \a reduceFunction is
398 \note This function will block until the iterator reaches the end of the
399 sequence being processed.
401 \sa blockingMappedReduced()