diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2021-02-24 17:17:42 +0100 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2021-02-25 16:08:43 +0100 |
commit | 5624b35d6514c5439b9d6dc639dc71228ca7b5ca (patch) | |
tree | 144fa1a939be81fd5e5c64faba43298166c7f382 | |
parent | c2bec047e281d949015ecd27daf2ba2d51b21526 (diff) |
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>
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp | 11 | ||||
-rw-r--r-- | src/corelib/thread/qfuture.qdoc | 10 |
2 files changed, 21 insertions, 0 deletions
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 a5e1a7f6e4..a53ca13cc6 100644 --- a/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp @@ -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] diff --git a/src/corelib/thread/qfuture.qdoc b/src/corelib/thread/qfuture.qdoc index f74901379f..b81944200e 100644 --- a/src/corelib/thread/qfuture.qdoc +++ b/src/corelib/thread/qfuture.qdoc @@ -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. |