Thread Support in Qt#
A detailed discussion of thread handling 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.
Multithreading Technologies in Qt
Synchronizing Threads
Reentrancy and Thread-Safety
Threads and QObjects
Thread-Support in Qt Modules
The Threading Classes#
These classes are relevant to threaded applications.
QtConcurrent.QTaskBuilderThe QTaskBuilder class is used for adjusting task parameters.
Selecting values from a sequence and combining them, all in parallel.
The QtConcurrent namespace provides high-level APIs that make it possible to write multi-threaded programs without using low-level threading primitives.
Transforming values from a sequence and combining them, all in parallel.
A simple way to run a task in a separate thread.
A configurable way to run a task in a separate thread.
QAtomicIntegerThe QAtomicInteger class provides platform-independent atomic operations on integers.
QAtomicPointerThe QAtomicPointer class is a template class that provides platform-independent atomic operations on pointers.
PySide6.QtCore.QFutureThe QFuture class represents the result of an asynchronous computation.
QtFuture.WhenAnyResultQtFuture::WhenAnyResult is used to represent the result of QtFuture::whenAny().
QFutureSynchronizerThe QFutureSynchronizer class is a convenience class that simplifies QFuture synchronization.
PySide6.QtCore.QFutureWatcherThe QFutureWatcher class allows monitoring a QFuture using signals and slots.
PySide6.QtCore.QMutexThe QMutex class provides access serialization between threads.
PySide6.QtCore.QRecursiveMutexThe QRecursiveMutex class provides access serialization between threads.
QMutexLockerThe QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes.
QPromiseThe QPromise class provides a way to store computation results to be accessed by QFuture.
PySide6.QtCore.QReadWriteLockThe QReadWriteLock class provides read-write locking.
PySide6.QtCore.QReadLockerThe QReadLocker class is a convenience class that simplifies locking and unlocking read-write locks for read access.
PySide6.QtCore.QWriteLockerThe QWriteLocker class is a convenience class that simplifies locking and unlocking read-write locks for write access.
PySide6.QtCore.QRunnableThe QRunnable class is the base class for all runnable objects.
PySide6.QtCore.QSemaphoreThe QSemaphore class provides a general counting semaphore.
PySide6.QtCore.QSemaphoreReleaserThe QSemaphoreReleaser class provides exception-safe deferral of a QSemaphore::release() call.
PySide6.QtCore.QThreadThe QThread class provides a platform-independent way to manage threads.
PySide6.QtCore.QThreadPoolThe QThreadPool class manages a collection of QThreads.
QThreadStorageThe QThreadStorage class provides per-thread data storage.
PySide6.QtCore.QWaitConditionThe QWaitCondition class provides a condition variable for synchronizing threads.
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.