What is Qt for boost :: prom <T>?
I see that Qt has a future class that is a direct analogue to boost :: future, but what is qt for boost :: promise ?
Strengthening :: promises means setting values ββin futures. In Qt, you cannot set futures; you can only return them. This is the only way to "tune" the data in the future.
So, in order to set the data in the future, you must return it from the function that QtConcurrent::run
called. To do this, you should use any of the Qt mechanisms for exchanging data between threads - events, mutate protected variables, etc. You must specify a thread that runs code that will return the future that is given in the future should be returned. This is the only way to achieve what I promised.
Alas, if you want to go into undocumented territory, then the following code does what boost::promise::setValue
:
QFuture<int> f; int i = 1; ... fdreportResult(&i); // or fdreportFinished(&i);
I did not bother to check if it works (for now).
Building my own QFuture, as shown in the accepted answer, did not help me. At first it seemed like it was working, but during testing, I realized that this does not block the caller. Oops! So I went a little deeper into the code and found that QFutureInterface is what you want to use as your "promise." Like boost :: prom, QFutureInterface is what you interact with in your workflow, and it is a factory for QFutures.
So, here is what I did in Qt 4.8 (not sure if this applies to later versions).
QFutureInterface<QVariant> promise; promise.reportStarted(); ... promise.reportResult(someVariant); promise.reportFinished();
Then in the client thread, assuming you have access to the QFutureInterface promise
QVariant result = promise.future().result();
The call to future()
is the factory method for creating a QFuture binding to your QFutureInterface. You should be able to get QFuture and call result()
on it later if you want.
There is no official equivalent to Qt, but there are several community libraries that implement promises (or similar templates):
- Ben Lau AsyncFuture ( Benlau / Asyncfuture )
Based on the undocumentedQFutureInterface
mentioned in one of the other answers .
And this is not just a promise model, but an observer model. - Benoit Walter QtPromise ( bwalter / qt-promise )
Partially based on Ben Lau Async Future. - Simon Brunel QtPromise ( simonbrunel / qtpromise )
- My QtPromise ( julrich / QtPromise )
Disclaimer: I am the author.
Promises for Qt are now also available with QML / JavaScript. By the way: https://v-play.net/updates/release-2-18-1-javascript-promises-for-rest-services-tinder-swipe-material-cards -qml-QSortFilterProxyModel-QML-YouTube player
Here is a sample code:
import VPlayApps 1.0 import QtQuick 2.0 App { Component.onCompleted: { var p1 = Promise.resolve(3); var p2 = 1337; var p3 = HttpRequest .get("http://httpbin.org/get") .then(function(resp) { return resp.body; }); var p4 = Promise.all([p1, p2, p3]); p4.then(function(values) { console.log(values[0]); // 3 console.log(values[1]); // 1337 console.log(values[2]); // resp.body }); } }
I created this library, which is tightly integrated with Qt and implements javascript-style promises:
https://github.com/juangburgos/QDeferred
This allows you to create a thread safe asynchronous API as follows:
multiplyNumbersInThread(3, 4) .fail([](int res) { Q_UNUSED(res); qDebug() << "multiplyPositiveNumbers failed!"; }) .done([](int res) { qDebug() << "multiplyPositiveNumbers succeded! Result :" << res; });
Hope you find it useful.