QThreadPool

The QThreadPool class manages a collection of QThreads. More...

 #include <QThreadPool>

Inherits: QObject.

Note: All functions in this class are thread-safe.

This class was introduced in Qt 4.4.

Properties

  • 1 property inherited from QObject

Public Functions

QThreadPool ( QObject * parent = 0 )
~QThreadPool ()
int activeThreadCount () const
int expiryTimeout () const
int maxThreadCount () const
void releaseThread ()
void reserveThread ()
void setExpiryTimeout ( int expiryTimeout )
void setMaxThreadCount ( int maxThreadCount )
void start ( QRunnable * runnable, int priority = 0 )
bool tryStart ( QRunnable * runnable )
void waitForDone ()
bool waitForDone ( int msecs )

Static Public Members

QThreadPool * globalInstance ()

Additional Inherited Members

  • 1 public slot inherited from QObject
  • 1 signal inherited from QObject
  • 8 protected functions inherited from QObject

Detailed Description

The QThreadPool class manages a collection of QThreads.

QThreadPool manages and recyles individual QThread objects to help reduce thread creation costs in programs that use threads. Each Qt application has one global QThreadPool object, which can be accessed by calling globalInstance().

To use one of the QThreadPool threads, subclass QRunnable and implement the run() virtual function. Then create an object of that class and pass it to QThreadPool::start().

 class HelloWorldTask : public QRunnable
 {
     void run()
     {
         qDebug() << "Hello world from thread" << QThread::currentThread();
     }
 }

 HelloWorldTask *hello = new HelloWorldTask();
 // QThreadPool takes ownership and deletes 'hello' automatically
 QThreadPool::globalInstance()->start(hello);

QThreadPool deletes the QRunnable automatically by default. Use QRunnable::setAutoDelete() to change the auto-deletion flag.

QThreadPool supports executing the same QRunnable more than once by calling tryStart(this) from within QRunnable::run(). If autoDelete is enabled the QRunnable will be deleted when the last thread exits the run function. Calling start() multiple times with the same QRunnable when autoDelete is enabled creates a race condition and is not recommended.

Threads that are unused for a certain amount of time will expire. The default expiry timeout is 30000 milliseconds (30 seconds). This can be changed using setExpiryTimeout(). Setting a negative expiry timeout disables the expiry mechanism.

Call maxThreadCount() to query the maximum number of threads to be used. If needed, you can change the limit with setMaxThreadCount(). The default maxThreadCount() is QThread::idealThreadCount(). The activeThreadCount() function returns the number of threads currently doing work.

The reserveThread() function reserves a thread for external use. Use releaseThread() when your are done with the thread, so that it may be reused. Essentially, these functions temporarily increase or reduce the active thread count and are useful when implementing time-consuming operations that are not visible to the QThreadPool.

Note that QThreadPool is a low-level class for managing threads, see QtConcurrent::run() or the other Qt Concurrent APIs for higher level alternatives.

See also QRunnable.

Member Function Documentation

QThreadPool::QThreadPool ( QObject * parent = 0 )

Constructs a thread pool with the given parent.

QThreadPool::~QThreadPool ()

Destroys the QThreadPool. This function will block until all runnables have been completed.

QThreadPool * QThreadPool::globalInstance () [static]

Returns the global QThreadPool instance.

void QThreadPool::releaseThread ()

Releases a thread previously reserved by a call to reserveThread().

Note: Calling this function without previously reserving a thread temporarily increases maxThreadCount(). This is useful when a thread goes to sleep waiting for more work, allowing other threads to continue. Be sure to call reserveThread() when done waiting, so that the thread pool can correctly maintain the activeThreadCount().

See also reserveThread().

void QThreadPool::reserveThread ()

Reserves one thread, disregarding activeThreadCount() and maxThreadCount().

Once you are done with the thread, call releaseThread() to allow it to be reused.

Note: This function will always increase the number of active threads. This means that by using this function, it is possible for activeThreadCount() to return a value greater than maxThreadCount() .

See also releaseThread().

void QThreadPool::start ( QRunnable * runnable, int priority = 0 )

Reserves a thread and uses it to run runnable, unless this thread will make the current thread count exceed maxThreadCount(). In that case, runnable is added to a run queue instead. The priority argument can be used to control the run queue's order of execution.

Note that the thread pool takes ownership of the runnable if runnable->autoDelete() returns true, and the runnable will be deleted automatically by the thread pool after the runnable->run() returns. If runnable->autoDelete() returns false, ownership of runnable remains with the caller. Note that changing the auto-deletion on runnable after calling this functions results in undefined behavior.

bool QThreadPool::tryStart ( QRunnable * runnable )

Attempts to reserve a thread to run runnable.

If no threads are available at the time of calling, then this function does nothing and returns false. Otherwise, runnable is run immediately using one available thread and this function returns true.

Note that the thread pool takes ownership of the runnable if runnable->autoDelete() returns true, and the runnable will be deleted automatically by the thread pool after the runnable->run() returns. If runnable->autoDelete() returns false, ownership of runnable remains with the caller. Note that changing the auto-deletion on runnable after calling this function results in undefined behavior.

void QThreadPool::waitForDone ()

Waits for each thread to exit and removes all threads from the thread pool.

bool QThreadPool::waitForDone ( int msecs )

This function overloads waitForDone().

Waits up to msecs milliseconds for all threads to exit and removes all threads from the thread pool. Returns true if all threads were removed; otherwise it returns false.

This function was introduced in Qt 4.8.

comments powered by Disqus