QFutureSynchronizer Class

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

Header: #include <QFutureSynchronizer>
qmake: QT += core
Since: Qt 4.4

Public Functions

QFutureSynchronizer()
QFutureSynchronizer(const QFuture<T> &future)
~QFutureSynchronizer()
void addFuture(const QFuture<T> &future)
bool cancelOnWait() const
void clearFutures()
QList<QFuture<T> > futures() const
void setCancelOnWait(bool enabled)
void setFuture(const QFuture<T> &future)
void waitForFinished()

Protected Variables

bool m_cancelOnWait
QList<QFuture<T> > m_futures

Detailed Description

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

QFutureSynchronizer is a template class that simplifies synchronization of one or more QFuture objects. Futures are added using the addFuture() or setFuture() functions. The futures() function returns a list of futures. Use clearFutures() to remove all futures from the QFutureSynchronizer.

The waitForFinished() function waits for all futures to finish. The destructor of QFutureSynchronizer calls waitForFinished(), providing an easy way to ensure that all futures have finished before returning from a function:


  void someFunction()
  {
      QFutureSynchronizer<void> synchronizer;

      ...

      synchronizer.addFuture(QtConcurrent::run(anotherFunction));
      synchronizer.addFuture(QtConcurrent::map(list, mapFunction));

      return; // QFutureSynchronizer waits for all futures to finish
  }

The behavior of waitForFinished() can be changed using the setCancelOnWait() function. Calling setCancelOnWait(true) will cause waitForFinished() to cancel all futures before waiting for them to finish. You can query the status of the cancel-on-wait feature using the cancelOnWait() function.

See also QFuture, QFutureWatcher, and Qt Concurrent.

Member Function Documentation

QFutureSynchronizer::QFutureSynchronizer()

Default constructs an instance of QFutureSynchronizer.

QFutureSynchronizer::QFutureSynchronizer(const QFuture<T> &future)

Default constructs an instance of QFutureSynchronizer.

QFutureSynchronizer::~QFutureSynchronizer()

Destroys the instance of QFutureSynchronizer.

void QFutureSynchronizer::addFuture(const QFuture<T> &future)

bool QFutureSynchronizer::cancelOnWait() const

See also setCancelOnWait().

void QFutureSynchronizer::clearFutures()

QList<QFuture<T> > QFutureSynchronizer::futures() const

void QFutureSynchronizer::setCancelOnWait(bool enabled)

See also cancelOnWait().

void QFutureSynchronizer::setFuture(const QFuture<T> &future)

void QFutureSynchronizer::waitForFinished()