本页内容

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

管理一组 QThread 对象

QThreadStorage

按线程的数据存储

QWaitCondition

用于线程同步的条件变量

QWriteLocker

用于简化读写锁锁定和解锁操作的便捷类(适用于写入访问)

QtConcurrent

高级 API,使开发者无需使用低级线程原语即可编写多线程程序

QtConcurrent::QTaskBuilder

用于调整任务参数

QtFuture::WhenAnyResult

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

QAtomicInteger::qYieldCpu()

注意:Qt 的 线程类是通过原生线程 API(例如 Win32 和 pthreads)实现的。因此,它们只能与使用相同原生 API 的线程配合使用。

© 2026 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.