diff options
author | Jarek Kobus <jaroslaw.kobus@qt.io> | 2020-09-30 16:42:47 +0200 |
---|---|---|
committer | Jarek Kobus <jaroslaw.kobus@qt.io> | 2020-10-28 15:38:52 +0100 |
commit | c34673e5d918e1882401572f81d8c2d6f54c5459 (patch) | |
tree | e85108896b9cd29a035384846d50d423d79e1c73 /src/concurrent | |
parent | cf043a785ac9357e8f1283ea9e1496261af2b1a5 (diff) |
QtConcurrent: Handle running with promise inside QTaskBuilder
Please note, that in case of run with promise it doesn't make
sense to provide the overload taking the FutureResult, as if we are
going to ignore the returned QFuture object, we can't communicate
with the passed QPromise.
Fixes: QTBUG-87083
Change-Id: I4066506736c2bbeea3e42030b9495f13e06cb27e
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Diffstat (limited to 'src/concurrent')
-rw-r--r-- | src/concurrent/doc/snippets/code/src_concurrent_qtconcurrenttask.cpp | 9 | ||||
-rw-r--r-- | src/concurrent/qtaskbuilder.h | 5 | ||||
-rw-r--r-- | src/concurrent/qtconcurrenttask.qdoc | 8 |
3 files changed, 20 insertions, 2 deletions
diff --git a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrenttask.cpp b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrenttask.cpp index b067b49e7e..267d6ebc1e 100644 --- a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrenttask.cpp +++ b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrenttask.cpp @@ -129,3 +129,12 @@ QtConcurrent::task([]{ return 42; }).withPriority(10).spawn(); //! [11] QtConcurrent::task([]{ qDebug("Hello, world!"); }).spawn(FutureResult::Ignore); //! [11] + +//! [12] +void increment(QPromise<int> &promise, int i) +{ + promise.addResult(i + 1); +} + +int result = QtConcurrent::task(&increment).withArguments(10).spawn().result(); // result == 11 +//! [12] diff --git a/src/concurrent/qtaskbuilder.h b/src/concurrent/qtaskbuilder.h index e64df242c3..16b21f103b 100644 --- a/src/concurrent/qtaskbuilder.h +++ b/src/concurrent/qtaskbuilder.h @@ -89,10 +89,11 @@ public: [[nodiscard]] auto spawn() { - return (new StoredFunctionCall<Task, Args...>(std::move(taskWithArgs))) - ->start(startParameters); + return TaskResolver<std::decay_t<Task>, std::decay_t<Args>...>::run( + std::move(taskWithArgs), startParameters); } + // We don't want to run with promise when we don't return a QFuture void spawn(FutureResult) { (new StoredFunctionCall<Task, Args...>(std::move(taskWithArgs))) diff --git a/src/concurrent/qtconcurrenttask.qdoc b/src/concurrent/qtconcurrenttask.qdoc index 9000b424fe..72951f8f11 100644 --- a/src/concurrent/qtconcurrenttask.qdoc +++ b/src/concurrent/qtconcurrenttask.qdoc @@ -148,6 +148,14 @@ QtConcurrent::QTaskBuilder::spawn(QtConcurrent::FutureResult::Ignore): \snippet code/src_concurrent_qtconcurrenttask.cpp 11 + + You can access the promise object associated with the task by defining an + additional argument of \c {QPromise<T> &} type inside the function. + This additional argument must be the first argument passed to the function, and + like in \l {Concurrent Run With Promise} mode, the function is expected to return void type. + Result reporting is done through QPromise API: + + \snippet code/src_concurrent_qtconcurrenttask.cpp 12 */ /*! |