Qt之线程的使用

多线程编程是一种常见的技术,它可以提高程序的性能和响应性。Qt 是一个广泛使用的跨平台 C++ 图形用户界面(GUI)库,它提供了方便的线程支持。在本文中,我们将探讨 Qt 线程的使用,并通过示例来展示如何在 Qt 应用程序中创建和管理线程。

首先,让我们了解一下线程的基本概念。线程是程序执行的独立路径,每个线程可以同时执行不同的任务。通过创建多个线程,我们可以并行处理多个任务,从而提高程序的执行效率。

在 Qt 中,可以使用QThread类来创建和管理线程。以下是一个简单的示例,展示了如何在 Qt 中使用线程:

复制代码
#include <QThread>
#include <QDebug>

// 要在线程中执行的任务函数
void taskFunction() {
    qDebug() << "Task running in thread";
}

int main() {
    // 创建一个新线程
    QThread thread;
    // 在新线程中执行任务函数
    QObject::connect(&thread, &QThread::started, []() {
        taskFunction();
    });
    
    thread.start();
    
    // 等待线程执行完成
    thread.waitForFinished();
    return 0;
}

在上述示例中,我们创建了一个QThread对象thread。然后,我们使用connect函数将QThread::started信号与一个匿名函数连接起来,该函数会在线程启动后执行taskFunction

接下来,我们使用start函数启动线程,线程将开始执行taskFunction

最后,我们使用waitForFinished函数等待线程执行完成。

请注意,在实际应用中,你可能需要在线程中处理更复杂的任务,并在适当的时候进行线程间的通信和同步。此外,还可以使用QMutexQSemaphore等同步对象来保护共享数据的访问。

另外,线程的创建和管理也需要谨慎处理。过度创建线程可能会导致系统资源的消耗和性能下降。在实际应用中,需要根据任务的特性和需求来合理规划线程的数量和使用方式。

除了手动创建线程,Qt 还提供了一些更高级的线程管理工具,例如QThreadPoolQtConcurrentQThreadPool可以方便地管理一组线程,并将任务分配到空闲的线程中执行。QtConcurrent提供了一些简化的并行计算接口,使得编写多线程代码更加简单和高效。

使用 QThreadPool::globalInstance() 线程池操作

QThreadPool用于管理和回收单个QThread对象,以帮助减少使用线程的程序中的线程创建成本。每个Qt应用程序都有一个全局QThreadPool对象,可以通过调用globalInstance()进行访问。

复制代码
class Task : public QRunnable
{
    void run() override
    {
        // 费时操作
        qDebug() << "thread run at:" << QThread::currentThread();
    }
};
 
auto *task = new Task();
QThreadPool::globalInstance()->start(task);

注意事项:

  • 默认情况下, 用户在调用 QThreadPool::globalInstance()->start(task); 之后不需要再关心 task 对象的释放问题,因为 QThreadPool 会自动删除 task。当然也可以通过 QRunnable :: setAutoDelete(bool) 调整策略。

  • 在一定时间内未使用的线程将过期。默认的到期超时为30000毫秒(30秒)。可以使用setExpiryTimeout()进行调整。当设置到期超时为负值时将禁用到期机制。

  • maxThreadCount()方法可以查询最大线程数,也可以使用setMaxThreadCount()进行调整。默认的maxThreadCount()QThread::idealThreadCount()

  • QThreadPool 是一个用于管理线程的低级类,高级玩法请参照 QtConcurrent 模块。

QtConcurrent模块。(高级玩法)

QtConcurrent模块扩展了Qt Core模块中提供的基本线程支持,并简化了可在所有可用CPU内核上并行执行的代码的开发。 The QtConcurrent namespace provides high-level APIs that make it possible to write multi-threaded programs without using low-level threading primitives such as mutexes, read-write locks, wait conditions, or semaphores. Programs written with QtConcurrent automatically adjust the number of threads used according to the number of processor cores available. This means that applications written today will continue to scale when deployed on multi-core systems in the future.

此模块简化了用户操作,强烈推荐使用。

下面是一个简单的示例。

复制代码
// 方法参数为 Function 
// 可以处理返回值
QFuture<int> res = QtConcurrent::run([](){ 
    // 费时操作
    return 0;
});
 
// 可以等待处理完成
// 1.阻塞执行 不阻塞事件循环
// while(!res.isFinished()) {
    // wait
    // QApplication::processEvents(QEventLoop::AllEvents, 200);
// }
// 2. 阻塞等待
res.waitForFinished();
 
// 处理返回值
int result = res.result();

注意事项:

  • 需要引入 Qt Concurrent模块。

  • 可以使用 QFutureWatcher 监视 QFuture。以下是一个官方示例:

    // Instantiate the objects and connect to the finished signal.
    MyClass myObject;
    QFutureWatcher watcher;
    connect(&watcher, QFutureWatcher::finished, &myObject, &MyClass::handleFinished);

    // Start the computation.
    QFuture future = QtConcurrent::run(...);
    watcher.setFuture(future);

码字不易,欢迎点赞支持!

相关推荐
磊 子4 分钟前
C++设计模式
javascript·c++·设计模式
Jinkxs6 分钟前
Rust 性能优化全流程:从 flamegraph 定位瓶颈到 unsafe 与 SIMD 加速,响应快 2 倍
开发语言·性能优化·rust
小短腿的代码世界27 分钟前
Qt定时器高精度架构:从QTimer源码到纳秒级定时调度
数据库·qt·架构
尘中远31 分钟前
Qt高性能绘图库QIm——实现二维三维科学绘图
开发语言·qt·信息可视化
雨辰AI31 分钟前
从零搭建大模型本地运行环境|Python+CUDA 基础配置避坑大全
大数据·开发语言·人工智能·python·ai·ai编程·ai写作
h_a_o777oah35 分钟前
【算法专项】扩展域并查集:原理详解及解决大部分种类并查集问题(洛谷P5937 P2024 C++代码)
数据结构·c++·算法·acm·并查集·扩展域·逻辑建模
DogDaoDao42 分钟前
【第 05 篇】Python的字典与集合
开发语言·python·集合·字典
兰令水1 小时前
leecodecode【单调栈】【2026.6.12打卡-java版本】
java·开发语言·算法
leagsoft_10031 小时前
零信任选型五刀法——零信任怎么选?五个问题,五条红线
开发语言·php
AI人工智能+电脑小能手1 小时前
【大白话说Java面试题 第112题】【并发篇】第12题:AQS 中节点的入队时机有哪些?
java·开发语言·面试