diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-02-27 17:30:07 +0100 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-03-05 13:24:32 +0100 |
commit | dfaca09e85a49d2983bb89893bfbe1ba4c19eab4 (patch) | |
tree | c01c0acca4e8c183b555876d203fb4faa261d893 /src/corelib/thread/qfutureinterface.h | |
parent | 1057d568bb921e378b9fc5eb6c95b39dd6dc94fa (diff) |
Add support for attaching continuations to QFuture
Added QFuture::then() methods to allow chaining multiple asynchronous
computations.
Continuations can use the following execution policies:
* QtFuture::Launch::Sync - the continuation will be launched in the same
thread in which the parent has been executing.
* QtFuture::Launch::Async - the continuation will be launched in a new
thread.
* QtFuture::Launch::Inherit - the continuation will inherit the launch
policy of the parent, or its thread pool (if it was using a custom one).
* Additionally then() also accepts a custom QThreadPool* instance.
Note, that if the parent future gets canceled, its continuation(s) will
be also canceled.
If the parent throws an exception, it will be propagated to the
continuation's future, unless it is caught inside the continuation
(if it has a QFuture arg).
Some example usages:
QFuture<int> future = ...;
future.then([](int res1){ ... }).then([](int res2){ ... })...
QFuture<int> future = ...;
future.then([](QFuture<int> fut1){ /* do something with fut1 */ })...
In the examples above all continuations will run in the same thread as
future.
QFuture<int> future = ...;
future.then(QtFuture::Launch::Async, [](int res1){ ... })
.then([](int res2){ ... })..
In this example the continuations will run in a new thread (but on the
same one).
QThreadPool pool;
QFuture<int> future = ...;
future.then(&pool, [](int res1){ ... })
.then([](int res2){ ... })..
In this example the continuations will run in the given thread pool.
[ChangeLog][QtCore] Added support for attaching continuations to QFuture.
Task-number: QTBUG-81587
Change-Id: I5b2e176694f7ae8ce00404aca725e9a170818955
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/corelib/thread/qfutureinterface.h')
-rw-r--r-- | src/corelib/thread/qfutureinterface.h | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/src/corelib/thread/qfutureinterface.h b/src/corelib/thread/qfutureinterface.h index 43dfd6bac4..c2e884911f 100644 --- a/src/corelib/thread/qfutureinterface.h +++ b/src/corelib/thread/qfutureinterface.h @@ -58,6 +58,11 @@ class QFutureInterfaceBasePrivate; class QFutureWatcherBase; class QFutureWatcherBasePrivate; +namespace QtPrivate { +template<typename Function, typename ResultType, typename ParentResultType> +class Continuation; +} + class Q_CORE_EXPORT QFutureInterfaceBase { public: @@ -68,7 +73,9 @@ public: Finished = 0x04, Canceled = 0x08, Paused = 0x10, - Throttled = 0x20 + Throttled = 0x20, + // Pending means that the future depends on another one, which is not finished yet + Pending = 0x40 }; QFutureInterfaceBase(State initialState = NoState); @@ -86,6 +93,7 @@ public: void setRunnable(QRunnable *runnable); void setThreadPool(QThreadPool *pool); + QThreadPool *threadPool() const; void setFilterMode(bool enable); void setProgressRange(int minimum, int maximum); int progressMinimum() const; @@ -141,6 +149,18 @@ private: private: friend class QFutureWatcherBase; friend class QFutureWatcherBasePrivate; + + template<typename Function, typename ResultType, typename ParentResultType> + friend class QtPrivate::Continuation; + +protected: + void setContinuation(std::function<void()> func); + void runContinuation() const; + + void setLaunchAsync(bool value); + bool launchAsync() const; + + bool isRunningOrPending() const; }; template <typename T> @@ -239,6 +259,7 @@ inline void QFutureInterface<T>::reportFinished(const T *result) if (result) reportResult(result); QFutureInterfaceBase::reportFinished(); + QFutureInterfaceBase::runContinuation(); } template <typename T> @@ -292,7 +313,11 @@ public: void reportResult(const void *, int) { } void reportResults(const QVector<void> &, int) { } - void reportFinished(const void * = nullptr) { QFutureInterfaceBase::reportFinished(); } + void reportFinished(const void * = nullptr) + { + QFutureInterfaceBase::reportFinished(); + QFutureInterfaceBase::runContinuation(); + } }; QT_END_NAMESPACE |