QFutureSynchronizer Class▲
-
Header: QFutureSynchronizer
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
-
qmake: QT += core
-
Group: QFutureSynchronizer is part of thread
Detailed Description▲
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&
lt;void
&
gt; 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▲
See also QFuture, QFutureWatcher, Qt Concurrent
Member Function Documentation▲
QFutureSynchronizer::QFutureSynchronizer()▲
Constructs a QFutureSynchronizer.
[default] QFutureSynchronizer::QFutureSynchronizer(const QFuture<T> &future)▲
Constructs a QFutureSynchronizer and begins watching future by calling addFuture().
See Also▲
See also addFuture()
QFutureSynchronizer::~QFutureSynchronizer()▲
Calls waitForFinished() function to ensure that all futures have finished before destroying this QFutureSynchronizer.
See Also▲
See also waitForFinished()
bool QFutureSynchronizer::cancelOnWait() const▲
Returns true if the cancel-on-wait feature is enabled; otherwise returns false. If cancel-on-wait is enabled, the waitForFinished() function will cancel all futures before waiting for them to finish.
See Also▲
See also setCancelOnWait(), waitForFinished()
void QFutureSynchronizer::clearFutures()▲
Removes all managed futures from this QFutureSynchronizer.
See Also▲
See also addFuture(), setFuture()
QList<QFuture<T>> QFutureSynchronizer::futures() const▲
void QFutureSynchronizer::setCancelOnWait(bool enabled)▲
Enables or disables the cancel-on-wait feature based on the enabled argument. If enabled is true, the waitForFinished() function will cancel all futures before waiting for them to finish.
See Also▲
See also cancelOnWait(), waitForFinished()
void QFutureSynchronizer::waitForFinished()▲
Waits for all futures to finish. If cancelOnWait() returns true, each future is canceled before waiting for them to finish.
See Also▲
See also cancelOnWait(), setCancelOnWait()