Qt 4.8
Public Functions | Protected Variables | List of all members
QFutureSynchronizer< T > Class Template Reference

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

#include <qfuturesynchronizer.h>

Public Functions

void addFuture (const QFuture< T > &future)
 Adds future to the list of managed futures. More...
 
bool cancelOnWait () const
 Returns true if the cancel-on-wait feature is enabled; otherwise returns false. More...
 
void clearFutures ()
 Removes all managed futures from this QFutureSynchronizer. More...
 
QList< QFuture< T > > futures () const
 Returns a list of all managed futures. More...
 
 QFutureSynchronizer ()
 Constructs a QFutureSynchronizer. More...
 
 QFutureSynchronizer (const QFuture< T > &future)
 Constructs a QFutureSynchronizer and begins watching future by calling addFuture(). More...
 
void setCancelOnWait (bool enabled)
 Enables or disables the cancel-on-wait feature based on the enabled argument. More...
 
void setFuture (const QFuture< T > &future)
 Sets future to be the only future managed by this QFutureSynchronizer. More...
 
void waitForFinished ()
 Waits for all futures to finish. More...
 
 ~QFutureSynchronizer ()
 Calls waitForFinished() function to ensure that all futures have finished before destroying this QFutureSynchronizer. More...
 

Protected Variables

bool m_cancelOnWait
 
QList< QFuture< T > > m_futures
 

Detailed Description

template<typename T>
class QFutureSynchronizer< T >

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

Since
4.4

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()
{
...
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, {Concurrent Programming}{Qt Concurrent}

Definition at line 55 of file qfuturesynchronizer.h.

Constructors and Destructors

◆ QFutureSynchronizer() [1/2]

template<typename T>
QFutureSynchronizer< T >::QFutureSynchronizer ( )
inline

Constructs a QFutureSynchronizer.

Definition at line 60 of file qfuturesynchronizer.h.

60 : m_cancelOnWait(false) { }

◆ QFutureSynchronizer() [2/2]

template<typename T>
QFutureSynchronizer< T >::QFutureSynchronizer ( const QFuture< T > &  future)
inlineexplicit

Constructs a QFutureSynchronizer and begins watching future by calling addFuture().

See also
addFuture()

Definition at line 61 of file qfuturesynchronizer.h.

62  : m_cancelOnWait(false)
63  { addFuture(future); }
void addFuture(const QFuture< T > &future)
Adds future to the list of managed futures.

◆ ~QFutureSynchronizer()

template<typename T>
QFutureSynchronizer< T >::~QFutureSynchronizer ( )
inline

Calls waitForFinished() function to ensure that all futures have finished before destroying this QFutureSynchronizer.

See also
waitForFinished()

Definition at line 64 of file qfuturesynchronizer.h.

64 { waitForFinished(); }
void waitForFinished()
Waits for all futures to finish.

Functions

◆ addFuture()

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

Adds future to the list of managed futures.

See also
futures()

Definition at line 73 of file qfuturesynchronizer.h.

Referenced by QFutureSynchronizer< T >::QFutureSynchronizer(), and QFutureSynchronizer< T >::setFuture().

74  {
75  m_futures.append(future);
76  }
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QList< QFuture< T > > m_futures

◆ cancelOnWait()

template<typename T>
bool QFutureSynchronizer< T >::cancelOnWait ( ) const
inline

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
waitForFinished()

Definition at line 106 of file qfuturesynchronizer.h.

107  {
108  return m_cancelOnWait;
109  }

◆ clearFutures()

template<typename T>
void QFutureSynchronizer< T >::clearFutures ( )
inline

Removes all managed futures from this QFutureSynchronizer.

See also
addFuture(), setFuture()

Definition at line 91 of file qfuturesynchronizer.h.

92  {
93  m_futures.clear();
94  }
void clear()
Removes all items from the list.
Definition: qlist.h:764
QList< QFuture< T > > m_futures

◆ futures()

template<typename T>
QList< QFuture< T > > QFutureSynchronizer< T >::futures ( ) const
inline

Returns a list of all managed futures.

See also
addFuture(), setFuture()

Definition at line 96 of file qfuturesynchronizer.h.

97  {
98  return m_futures;
99  }
QList< QFuture< T > > m_futures

◆ setCancelOnWait()

template<typename T>
void QFutureSynchronizer< T >::setCancelOnWait ( bool  enabled)
inline

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
waitForFinished()

Definition at line 101 of file qfuturesynchronizer.h.

102  {
104  }
#define enabled

◆ setFuture()

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

Sets future to be the only future managed by this QFutureSynchronizer.

This is a convenience function that calls waitForFinished(), then clearFutures(), and finally passes future to addFuture().

See also
addFuture(), waitForFinished(), clearFutures()

Definition at line 66 of file qfuturesynchronizer.h.

67  {
69  m_futures.clear();
70  addFuture(future);
71  }
void addFuture(const QFuture< T > &future)
Adds future to the list of managed futures.
void waitForFinished()
Waits for all futures to finish.
void clear()
Removes all items from the list.
Definition: qlist.h:764
QList< QFuture< T > > m_futures

◆ waitForFinished()

template<typename T>
void QFutureSynchronizer< T >::waitForFinished ( )
inline

Waits for all futures to finish.

If cancelOnWait() returns true, each future is canceled before waiting for them to finish.

See also
cancelOnWait(), setCancelOnWait()

Definition at line 78 of file qfuturesynchronizer.h.

Referenced by QFutureSynchronizer< T >::setFuture(), and QFutureSynchronizer< T >::~QFutureSynchronizer().

79  {
80  if (m_cancelOnWait) {
81  for (int i = 0; i < m_futures.count(); ++i) {
82  m_futures[i].cancel();
83  }
84  }
85 
86  for (int i = 0; i < m_futures.count(); ++i) {
87  m_futures[i].waitForFinished();
88  }
89  }
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QList< QFuture< T > > m_futures

Properties

◆ m_cancelOnWait

template<typename T>
bool QFutureSynchronizer< T >::m_cancelOnWait
protected

◆ m_futures

template<typename T>
QList<QFuture<T> > QFutureSynchronizer< T >::m_futures
protected

The documentation for this class was generated from the following files: