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 ?

+5
source share
5 answers

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).

+4
source

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.

+10
source

There is no official equivalent to Qt, but there are several community libraries that implement promises (or similar templates):

+1
source

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 }); } } 
0
source

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.

0
source

Source: https://habr.com/ru/post/1415866/


All Articles