QThreadPool Class

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

Header: #include <QThreadPool>
qmake: QT += core
Since: Qt 4.4
Inherits: QObject

This class was introduced in Qt 4.4.

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

Properties

Public Functions

int activeThreadCount() const
int expiryTimeout() const
int maxThreadCount() const
void setExpiryTimeout(int expiryTimeout)
void setMaxThreadCount(int maxThreadCount)
void setStackSize(uint stackSize)
uint stackSize() const

Detailed Description

QThreadPool manages and recycles 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() override
     {
         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 the Qt Concurrent module for higher level alternatives.

See also QRunnable.

Property Documentation

activeThreadCount : const int

This property represents the number of active threads in the thread pool.

Note: It is possible for this function to return a value that is greater than maxThreadCount(). See reserveThread() for more details.

Access functions:

int activeThreadCount() const

See also reserveThread() and releaseThread().

expiryTimeout : int

Threads that are unused for expiryTimeout milliseconds are considered to have expired and will exit. Such threads will be restarted as needed. The default expiryTimeout is 30000 milliseconds (30 seconds). If expiryTimeout is negative, newly created threads will not expire, e.g., they will not exit until the thread pool is destroyed.

Note that setting expiryTimeout has no effect on already running threads. Only newly created threads will use the new expiryTimeout. We recommend setting the expiryTimeout immediately after creating the thread pool, but before calling start().

Access functions:

int expiryTimeout() const
void setExpiryTimeout(int expiryTimeout)

maxThreadCount : int

This property represents the maximum number of threads used by the thread pool.

Note: The thread pool will always use at least 1 thread, even if maxThreadCount limit is zero or negative.

The default maxThreadCount is QThread::idealThreadCount().

Access functions:

int maxThreadCount() const
void setMaxThreadCount(int maxThreadCount)

stackSize : uint

This property contains the stack size for the thread pool worker threads.

The value of the property is only used when the thread pool creates new threads. Changing it has no effect for already created or running threads.

The default value is 0, which makes QThread use the operating system default stack size.

This property was introduced in Qt 5.10.

Access functions:

uint stackSize() const
void setStackSize(uint stackSize)