Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Qt for boost::promise<T>?

I see that Qt has future class that is direct analog for boost::future but what is qt for boost::promise?

like image 813
myWallJSON Avatar asked Jun 03 '12 22:06

myWallJSON


1 Answers

Constructing my own QFuture as shown in the accepted answer did not work for me. At first it seemed like it was working, but in my testing I realized it was not blocking the caller. Whoops! So I dug into the code a little further and found that QFutureInterface is what you want to use as your 'promise'. Like boost::promise, QFutureInterface is what you interact with in your worker thread, and it is a factory for QFutures.

So here's what I've been doing in Qt 4.8 (not sure if this is applicable 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 future() call is a factory method for creating a QFuture bound to your QFutureInterface. You should be able to get the QFuture and call result() on it later if you wanted.

like image 65
BrandonLWhite Avatar answered Oct 19 '22 22:10

BrandonLWhite