Improve docs for QFuture::then() with context

Be more precise about attaching a continuation with the default
(QtFuture::Launch::Sync) launch policy after a continuation with
context.

Pick-to: 6.1
Change-Id: I5b80063df2443e5742033864ba012bf34ed4cdf7
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Sona Kurazyan 2021-02-24 17:17:42 +01:00
parent c2bec047e2
commit 5624b35d65
2 changed files with 21 additions and 0 deletions

View File

@ -283,3 +283,14 @@ auto future = QtConcurrent::run([] {
// Update UI elements
});
//! [19]
//! [20]
QObject *context = ...;
auto parentFuture = cachedResultsReady ? QtFuture::makeReadyFuture(results)
: QtConcurrent::run([] { /* compute results */});
auto future = parentFuture.then(context, [] (Results results) {
// Runs in the context's thread
}).then([] {
// May or may not run in the context's thread
});
//! [20]

View File

@ -1174,6 +1174,16 @@
This is because by default \c .then() is invoked from the same thread as the parent.
But note that if the continuation is attached after the parent has already finished,
it will be invoked in the thread where the parent future lives:
\snippet code/src_corelib_thread_qfuture.cpp 20
In the above example if \c cachedResultsReady is \c true, and a ready future is
returned, it is possible that the first \c .then() finishes before the second one
is attached. In this case it will be resolved in the current thread. Therefore, when
in doubt, pass the context explicitly.
\note When calling this method, it should be guaranteed that the \a context stays alive
throughout the execution of the chain.