QPromise Class

template <typename T> class QPromise

The QPromise class provides a way to store computation results to be accessed by QFuture. More...

Header: #include <QPromise>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
Since: Qt 6.0

Note: All functions in this class are thread-safe.

Detailed Description

QPromise provides a simple way to communicate progress and results of the user-defined computation to QFuture in an asynchronous fashion. For the communication to work, QFuture must be constructed by QPromise.

You can use QPromise based workloads as an alternative to Qt Concurrent framework when fine-grained control is needed or high-level communication primitive to accompany QFuture is sufficient.

The simplest case of promise and future collaboration would be a single result communication:

     QPromise<int> promise;
     QFuture<int> future = promise.future();

     const std::unique_ptr<QThread> thread(QThread::create([] (QPromise<int> promise) {
         promise.start();   // notifies QFuture that the computation is started
         promise.addResult(42);
         promise.finish();  // notifies QFuture that the computation is finished
     }, std::move(promise)));
     thread->start();

     future.waitForFinished();  // blocks until QPromise::finish is called
     future.result();  // returns 42

By design, QPromise is a move-only object. This behavior helps to ensure that whenever the promise is destroyed, the associated future object is notified and will not wait forever for the results to become available. However, this is inconvenient if one wants to use the same promise to report results from different threads. There is no specific way to do that at the moment, but known mechanisms exist, such as the use of smart pointers or raw pointers/references. QSharedPointer is a good default choice if you want to copy your promise and use it in multiple places simultaneously. Raw pointers or references are, in a sense, easier, and probably perform better (since there is no need to do a resource management) but may lead to dangling.

Here is an example of how a promise can be used in multiple threads:

     const auto sharedPromise = std::make_shared<QPromise<int>>();
     QFuture<int> future = sharedPromise->future();

     // ...

     sharedPromise->start();

     // here, QPromise is shared between threads via a smart pointer
     const std::unique_ptr<QThread> threads[] = {
         std::unique_ptr<QThread>(QThread::create([] (auto sharedPromise) {
             sharedPromise->addResult(0, 0);  // adds value 0 by index 0
         }, sharedPromise)),
         std::unique_ptr<QThread>(QThread::create([] (auto sharedPromise) {
             sharedPromise->addResult(-1, 1);  // adds value -1 by index 1
         }, sharedPromise)),
         std::unique_ptr<QThread>(QThread::create([] (auto sharedPromise) {
             sharedPromise->addResult(-2, 2);  // adds value -2 by index 2
         }, sharedPromise)),
         // ...
     };
     // start all threads
     for (auto& t : threads)
         t->start();

     // ...

     future.resultAt(0);  // waits until result at index 0 becomes available. returns value  0
     future.resultAt(1);  // waits until result at index 1 becomes available. returns value -1
     future.resultAt(2);  // waits until result at index 2 becomes available. returns value -2

     sharedPromise->finish();

See also QFuture.