Qt 中的线程支持

Qt 以平台独立线程类、线程安全的事件发布方式和跨线程的信号槽连接等形式提供线程支持。这使得开发可移植的多线程 Qt 应用程序和利用多处理器的优势变得容易。多线程编程也是一种有用的范例,可在不冻结应用程序用户界面的情况下执行耗时操作。

早期版本的 Qt 提供了不支持线程的构建库选项。自 Qt 4.0 起,线程始终处于启用状态。

主题:

这些文章假定读者已具备多线程应用程序的基本知识。

线程类

这些类与线程应用程序相关。

Concurrent Filter and Filter-Reduce

并行地从序列中选择值并组合它们

Concurrent Map and Map-Reduce

并行地从序列中转换值并组合它们

Concurrent Run

在单独线程中运行任务的简单方法

Concurrent Task

在独立线程中运行任务的可配置方式

QAtomicInteger

独立于平台的整数原子操作

QAtomicPointer

对指针进行独立于平台的原子运算的模板类

QFuture

代表异步计算的结果

QFutureSynchronizer

简化 QFuture 同步的便利类

QFutureWatcher

允许使用信号和插槽监控 QFuture

QMutex

线程间的访问序列化

QMutexLocker

简化锁定和解锁互斥的便利类

QPromise

存储 QFuture 可访问的计算结果的方法

QReadLocker

简化读取访问的读写锁锁定和解锁的便利类

QReadWriteLock

读写锁

QRecursiveMutex

线程间的访问序列化

QRunnable

所有可运行对象的基类

QSemaphore

通用计数信号

QSemaphoreReleaser

以异常安全方式延迟调用 QSemaphore::release()

QThread

与平台无关的线程管理方式

QThreadPool

管理 QThreads 集合

QThreadStorage

每线程数据存储

QWaitCondition

用于同步线程的条件变量

QWriteLocker

简化锁定和解锁读写锁(用于写访问)的便利类

QtConcurrent

高级应用程序接口,无需使用低级线程原语即可编写多线程程序

QtConcurrent::QTaskBuilder

用于调整任务参数

QtFuture::WhenAnyResult

QtFuture::WhenAnyResult 用于表示 QtFuture::whenAny() 的结果。

QAtomicInteger::qYieldCpu()

注意: Qt 的线程类是用本地线程 API 实现的,如 Win32 和 pthreads。因此,它们可以与相同本地 API 的线程一起使用。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.