6.0.0-rc4Source

QFutureSynchronizer Class

template <typename T = void> class QFutureSynchronizer

The QFutureSynchronizer class is a convenience class that simplifies QFuture synchronization. More…

Header#include <QFutureSynchronizer>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt 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()
voidsetFuture(const QFuture<T> &future)
voidaddFuture(const QFuture<T> &future)
voidwaitForFinished()
voidclearFutures()
QList<QFuture<T>>futures() const
voidsetCancelOnWait(bool enabled)
boolcancelOnWait() 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.

[explicit] 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().