![]() |
Home · Examples |
The <QtConcurrentRun> header provides a way to run a function in a separate thread. This function is a part of the Qt Concurrent framework.
The QtConcurrent::run() function runs a function in a separate thread. The return value of the function is made available through the QFuture API.Running a Function in a Separate Thread
To run a function in another thread, use QtConcurrent::run():
The following code example is written in c++.
extern void aFunction();
QFuture<void> future = QtConcurrent::run(aFunction);
This will run aFunction in a separate thread obtained from the default QThreadPool. You can use the QFuture and QFutureWatcher classes to monitor the status of the function.Passing Arguments to the Function
Passing arguments to the function is done by adding them to the QtConcurrent::run() call immediately after the function name. For example:
The following code example is written in c++.
extern void aFunctionWithArguments(int arg1, double arg2, const QString &string);
int integer = ...;
double floatingPoint = ...;
QString string = ...;
QFuture<void> future = QtConcurrent::run(aFunctionWithArguments, integer, floatingPoint, string);
A copy of each argument is made at the point where QtConcurrent::run() is called, and these values are passed to the thread when it begins executing the function. Changes made to the arguments after calling QtConcurrent::run() are not visible to the thread.Returning Values from the Function
Any return value from the function is available via QFuture:
The following code example is written in c++.
extern QString functionReturningAString(); QFuture<QString> future = QtConcurrent::run(functionReturningAString); ... QString result = future.result();As documented above, passing arguments is done like this:
extern QString someFunction(const QByteArray &input); QByteArray bytearray = ...; QFuture<QString> future = QtConcurrent::run(someFunction, bytearray); ... QString result = future.result();Note that the QFuture::result() function blocks and waits for the result to become available. Use QFutureWatcher to get notification when the function has finished execution and the result is available.
For example, calling QString::split() (a const member function) in a separate thread is done like this:
The following code example is written in c++.
// call 'QStringList QString::split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const' in a separate thread QString string = ...; QFuture<QStringList> future = QtConcurrent::run(string, &QString::split, QString(", "), QString::KeepEmptyParts, Qt::CaseSensitive); ... QStringList result = future.result();Calling a non-const member function is done like this:
// call 'void QImage::invertPixels(InvertMode mode)' in a separate thread QImage image = ...; QFuture<void> future = QtConcurrent::run(image, &QImage::invertPixels, QImage::InvertRgba); ... future.waitForFinished(); // At this point, the pixels in 'image' have been inverted
You can use boost::bind() or std::tr1::bind() to bind a number of arguments to a function when called. There are number of reasons for doing this:
Calling a bound function is done like this:
The following code example is written in c++.
void someFunction(int arg1, double arg2); QFuture<void> future = QtConcurrent::run(boost::bind(someFunction, 1, 2.0)); ...
Copyright © 2008 Trolltech | Trademarks | Qt Jambi 4.4.2_01 |