Thread Support in Qt

Qt provides thread support in the form of platform-independent threading classes, a thread-safe way of posting events, and signal-slot connections across threads. This makes it easy to develop portable multithreaded Qt applications and take advantage of multiprocessor machines. Multithreaded programming is also a useful paradigm for performing time-consuming operations without freezing the user interface of an application.

Earlier versions of Qt offered an option to build the library without thread support. Since Qt 4.0, threads are always enabled.

Topics:

These articles assume that the reader has basic knowledge about multithreaded applications.

The Threading Classes

These classes are relevant to threaded applications.

Concurrent Filter and Filter-Reduce

Selecting values from a sequence and combining them, all in parallel

Concurrent Map and Map-Reduce

Transforming values from a sequence and combining them, all in parallel

Concurrent Run

A simple way to run a task in a separate thread

Concurrent Task

A configurable way to run a task in a separate thread

QAtomicInteger

Platform-independent atomic operations on integers

QAtomicPointer

Template class that provides platform-independent atomic operations on pointers

QFuture

Represents the result of an asynchronous computation

QFutureSynchronizer

Convenience class that simplifies QFuture synchronization

QFutureWatcher

Allows monitoring a QFuture using signals and slots

QMutex

Access serialization between threads

QMutexLocker

Convenience class that simplifies locking and unlocking mutexes

QPromise

Way to store computation results to be accessed by QFuture

QReadLocker

Convenience class that simplifies locking and unlocking read-write locks for read access

QReadWriteLock

Read-write locking

QRecursiveMutex

Access serialization between threads

QRunnable

The base class for all runnable objects

QSemaphore

General counting semaphore

QSemaphoreReleaser

Exception-safe deferral of a QSemaphore::release() call

QThread

Platform-independent way to manage threads

QThreadPool

Manages a collection of QThreads

QThreadStorage

Per-thread data storage

QWaitCondition

Condition variable for synchronizing threads

QWriteLocker

Convenience class that simplifies locking and unlocking read-write locks for write access

QtConcurrent

High-level APIs that make it possible to write multi-threaded programs without using low-level threading primitives

QtConcurrent::QTaskBuilder

Used for adjusting task parameters

QtFuture::WhenAnyResult

QtFuture::WhenAnyResult is used to represent the result of QtFuture::whenAny()

QAtomicInteger::qYieldCpu()

Note: Qt's threading classes are implemented with native threading APIs; e.g., Win32 and pthreads. Therefore, they can be used with threads of the same native API.