QFutureSynchronizer Class
template <typename T = void> class QFutureSynchronizer
The QFutureSynchronizer class is a convenience class that simplifies QFuture synchronization. More…
| Header | #include <QFutureSynchronizer> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Functions
| QFutureSynchronizer() | |
| QFutureSynchronizer(const QFuture<T> &future) | |
| QFutureSynchronizer(QFutureSynchronizer &&other) | |
| QFutureSynchronizer & | operator=(QFutureSynchronizer &&other) |
| ~QFutureSynchronizer() | |
| void | setFuture(const QFuture<T> &future) |
| void | addFuture(const QFuture<T> &future) |
| void | waitForFinished() |
| void | clearFutures() |
| QList<QFuture<T>> | futures() const |
| void | setCancelOnWait(bool enabled) |
| bool | cancelOnWait() const |
Detailed Description
A synchronizer collects futures and waits for all of them when it is destroyed, so that the code that started the computations does not leave their data behind. With setCancelOnWait() it cancels them first.
{
QFutureSynchronizer<void> sync;
sync.addFuture(startJob(one));
sync.addFuture(startJob(two));
} // waits for both jobs
See also QFuture and QFutureWatcher.
Differences from Qt
The destructor does not throw: an exception that a future ended with is dropped there, and thrown only by waitForFinished(). There is no setFutures().
Member Function Documentation
QFutureSynchronizer::QFutureSynchronizer()
Constructs a synchronizer that holds no futures.
QFutureSynchronizer::QFutureSynchronizer(const QFuture<T> &future)
Constructs a synchronizer that holds future.
See also addFuture().
QFutureSynchronizer::QFutureSynchronizer(QFutureSynchronizer &&other)
Moves the futures of other into this synchronizer. other holds none afterwards.
QFutureSynchronizer::~QFutureSynchronizer()
Waits for all the futures, after canceling them if cancelOnWait() is true. An exception that a future ended with is not thrown from here.
See also waitForFinished().
void QFutureSynchronizer::addFuture(const QFuture<T> &future)
Adds future to the futures that the synchronizer holds.
See also setFuture() and futures().
bool QFutureSynchronizer::cancelOnWait() const
Returns true if the futures are canceled before waiting for them.
See also setCancelOnWait().
void QFutureSynchronizer::clearFutures()
Forgets the futures without waiting for them.
See also waitForFinished().
QList<QFuture<T>> QFutureSynchronizer::futures() const
Returns the futures that the synchronizer holds.
See also addFuture().
QFutureSynchronizer & QFutureSynchronizer::operator=(QFutureSynchronizer &&other)
Waits for the futures that this synchronizer holds, as the destructor does, and then takes over those of other. Returns a reference to this synchronizer.
void QFutureSynchronizer::setCancelOnWait(bool enabled)
Sets whether the futures are canceled before waitForFinished() or the destructor waits for them to enabled. It is false by default.
See also cancelOnWait().
void QFutureSynchronizer::setFuture(const QFuture<T> &future)
Waits for the futures that the synchronizer holds, forgets them, and then holds future only.
See also addFuture().
void QFutureSynchronizer::waitForFinished()
Waits for all the futures that the synchronizer holds, after canceling them if cancelOnWait() is true. It throws the exception of the first future that ended with one, after it has waited for all.
See also clearFutures() and setCancelOnWait().