From 102f7d31c469a546f52c930a047bd294fb198186 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Thu, 4 Nov 2021 17:01:54 +0100 Subject: Add support for combining multiple QFutures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [ChangeLog][QtCore] Added QtFuture::whenAll() and QtFuture::whenAny() functions, returning a QFuture that becomes ready when all or any of the supplied futures complete. Task-number: QTBUG-86714 Change-Id: I2bb7dbb4cdc4f79a7a4fd494142df6a0f93a2b39 Reviewed-by: Edward Welbourne Reviewed-by: MÃ¥rten Nordheim --- .../snippets/code/src_corelib_thread_qfuture.cpp | 87 ++++++++++++++++++++++ 1 file changed, 87 insertions(+) (limited to 'src/corelib/doc') diff --git a/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp b/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp index b8a4e708a5..176fb4a043 100644 --- a/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp @@ -311,3 +311,90 @@ auto resultFuture = testFuture.then([](int res) { return -1; }); //! [21] + +//! [22] +QList> inputFutures {...}; + +// whenAll has type QFuture>> +auto whenAll = QtFuture::whenAll(inputFutures.begin(), inputFutures.end()); + +// whenAllVector has type QFuture>> +auto whenAllVector = + QtFuture::whenAll>>(inputFutures.begin(), inputFutures.end()); +//! [22] + +//! [23] +QList> inputFutures {...}; + +QtFuture::whenAll(inputFutures.begin(), inputFutures.end()) + .then([](const QList> &results) { + for (auto future : results) { + if (future.isCanceled()) + // handle the cancellation (possibly due to an exception) + else + // do something with the result + } + }); +//! [23] + +//! [24] + +QFuture intFuture = ...; +QFuture stringFuture = ...; +QFuture voidFuture = ...; + +using FuturesVariant = std::variant, QFuture, QFuture>; + +// whenAll has type QFuture> +auto whenAll = QtFuture::whenAll(intFuture, stringFuture, voidFuture); + +// whenAllVector has type QFuture> +auto whenAllVector = + QtFuture::whenAll>(intFuture, stringFuture, voidFuture); + +//! [24] + +//! [25] +QFuture intFuture = ...; +QFuture stringFuture = ...; +QFuture voidFuture = ...; + +using FuturesVariant = std::variant, QFuture, QFuture>; + +QtFuture::whenAll(intFuture, stringFuture, voidFuture) + .then([](const QList &results) { + ... + for (auto result : results) + { + // assuming handleResult() is overloaded based on the QFuture type + std::visit([](auto &&future) { handleResult(future); }, result); + } + ... + }); +//! [25] + +//! [26] +QList> inputFutures = ...; + +QtFuture::whenAny(inputFutures.begin(), inputFutures.end()) + .then([](const QtFuture::WhenAnyResult &result) { + qsizetype index = result.index; + QFuture future = result.future; + // ... + }); +//! [26] + +//! [27] +QFuture intFuture = ...; +QFuture stringFuture = ...; +QFuture voidFuture = ...; + +using FuturesVariant = std::variant, QFuture, QFuture>; + +QtFuture::whenAny(intFuture, stringFuture, voidFuture).then([](const FuturesVariant &result) { + ... + // assuming handleResult() is overloaded based on the QFuture type + std::visit([](auto &&future) { handleResult(future); }, result); + ... +}); +//! [27] -- cgit v1.2.3