Qt开发 | Qt创建线程 | Qt并发-QtConcurrent

文章目录

  • 一、Qt创建线程的三种方法
  • 二、Qt并发:QtConcurrent介绍
  • 三、QtConcurrent run参数说明
  • 四、获取QtConcurrent的返回值
  • 五、C++其他线程技术介绍

一、Qt创建线程的三种方法

  以下是Qt创建线程的三种方法:

  • 方法一:派生于QThread

    派生于QThread,这是Qt创建线程最常用的方法。线程类中重写虚函数void QThread::run();,在run()函数中写具体内容,外部通过start()调用,即可执行线程体run()

    注意:派生于QThread的类,构造函数属于主线程,run函数属于子线程,可以通过打印线程id来判断

    示例:

    VS中创建Qt控制台项目

    MyThread.h

    #pragma once#include <QThread>class MyThread : public QThread
    {
    public:MyThread();void run()override;
    };
    

    MyThread.cpp

    #include "MyThread.h"
    #include <QDebug>MyThread::MyThread()
    {qDebug() << "MyThread construct id =" << QThread::currentThreadId;
    }void MyThread::run()
    {qDebug() << "MyThread run id =" << QThread::currentThreadId;int index = 0;while (1){qDebug() << index++;QThread::msleep(500);}
    }
    

    main.cpp

    #include <QtCore/QCoreApplication>
    #include <QDebug>
    #include "MyThread.h"int main(int argc, char *argv[])
    {QCoreApplication a(argc, argv);qDebug() << "main thread id = " << QThread::currentThreadId;MyThread th;th.start();qDebug() << "main thread end!";return a.exec();
    }
    

    运行结果

    main thread id =  0x78272815
    MyThread construct id = 0x78272815
    main thread end!
    MyThread run id = 0x78272815
    0
    1
    2
    3
    4
    5
    6
    ...
    
  • 方法二:派生于QRunnable

    派生于QRunnable,重写run()方法,在run()方法里处理其他任务,调用时需要借助Qt线程池

    注意:这种新建线程的方法的最大缺点是:不能使用Qt信号槽机制,因为QRunnable不是继承自QObject。但是这种方法的好处是:可以让QThreadPool来管理线程,QThreadPool会自动清理我们新建的QRunnable对象。

    示例:

    MyRunnable.h

    #pragma once#include <QRunnable>class MyRunnable : public QRunnable
    {
    public:MyRunnable();void run()override;
    };
    

    MyRunnable.cpp

    #include "MyRunnable.h"
    #include <QThread>
    #include <QDebug>MyRunnable::MyRunnable()
    {qDebug() << "MyRunnable construct id =" << QThread::currentThreadId;
    }void MyRunnable::run()
    {qDebug() << "MyRunnable run id =" << QThread::currentThreadId;
    }
    

    main.cpp

    #include <QtCore/QCoreApplication>
    #include <QDebug>
    #include <QThreadPool>
    #include "MyThread.h"
    #include "MyRunnable.h"int main(int argc, char *argv[])
    {QCoreApplication a(argc, argv);qDebug() << "main thread id = " << QThread::currentThreadId;//MyThread th;//th.start();MyRunnable* th = new MyRunnable();QThreadPool::globalInstance()->start(th);qDebug() << "main thread end!";return a.exec();
    }
    

    运行结果

    main thread id =  0x78272815
    MyRunnable construct id = 0x78272815
    main thread end!
    MyRunnable run id = 0x78272815
    
  • 方法三:moveToThread

    派生于QObject,使用moveToThread方法将任何QObject派生类的实例移动到另一个线程。将QThread对象作为私有成员,在构造函数里moveToThread,然后启动线程

    QThread *thread = new QThread;
    this->moveToThread(thread);
    thread->start();
    

    示例:

    MyObject.h

    #pragma once
    #include <qobject.h>
    #include <QThread>class MyObject : public QObject
    {Q_OBJECT
    public:MyObject();public slots:void process();private:QThread m_pTh;
    };
    

    MyObject.cpp

    #include "MyObject.h"
    #include <QDebug>MyObject::MyObject()
    {this->moveToThread(&m_pTh);m_pTh.start();qDebug() << "MyObject construct id = " << QThread::currentThreadId();
    }void MyObject::process()
    {qDebug() << "MyObject process id = " << QThread::currentThreadId();int index = 0;while (1){qDebug() << index++;QThread::msleep(300);}
    }
    

    ch7_1_mtt.h

    #pragma once#include <QtWidgets/QWidget>
    #include "ui_ch7_1_mtt.h"
    #include "MyObject.h"class ch7_1_mtt : public QWidget
    {Q_OBJECTpublic:ch7_1_mtt(QWidget *parent = nullptr);~ch7_1_mtt();public slots:void on_pushButton_clicked();signals:void sig_pro();private:Ui::ch7_1_mttClass ui;MyObject* m_pObj;
    };
    

    ch7_1_mtt.cpp

    #include "ch7_1_mtt.h"
    #include <QDebug>ch7_1_mtt::ch7_1_mtt(QWidget *parent): QWidget(parent)
    {ui.setupUi(this);qDebug() << "main construct id = " << QThread::currentThreadId();m_pObj = new MyObject();connect(this, &ch7_1_mtt::sig_pro, m_pObj, &MyObject::process);
    }ch7_1_mtt::~ch7_1_mtt()
    {}void ch7_1_mtt::on_pushButton_clicked()
    {//通过信号槽方式调用,相当于在子线程中运行;若直接调用process()函数,则相当于在主线程中运行emit sig_pro();
    }
    

    在槽函数中进行耗时操作可以使用线程,在子线程中进行耗时操作

    main.cpp

    #include "ch7_1_mtt.h"
    #include <QtWidgets/QApplication>int main(int argc, char *argv[])
    {QApplication a(argc, argv);ch7_1_mtt w;w.show();return a.exec();
    }
    

    运行结果

    main construct id = 0x52dc
    MyObject construct id =  0x52dc
    MyObject process id =  0x78272815
    0
    1
    2
    3
    4
    5
    6
    7
    

二、Qt并发:QtConcurrent介绍

  QtConcurrent是Qt框架中的一个模块,用于简化并发和多线程编程。它提供了一种高效的方式来执行并行任务,而无需直接处理线程的复杂性。QtConcurrent基于模板和函数式编程,使得编写并发代码更加简洁和易于理解。以下是QtConcurrent的一些关键特性:

  • 基于模板的并行算法:QtConcurrent提供了一系列的并行算法,如QtConcurrent::map()QtConcurrent::filter()QtConcurrent::reduce()等,它们可以并行地对数据集执行操作。
  • 基于Qt的线程池管理:QtConcurrent使用Qt的线程池来管理线程,这意味着开发者不需要手动创建和销毁线程,从而简化了线程管理。
  • 信号和槽的集成:QtConcurrent可以与Qt的信号和槽机制无缝集成,使得并行任务的结果可以很容易地通过信号传递给其他对象。
  • 懒加载和任务取消:QtConcurrent支持懒加载,即任务只有在实际需要结果时才开始执行。此外,它还支持取消正在执行的任务。
  • 异常处理:QtConcurrent可以处理并行执行过程中抛出的异常,确保程序的健壮性。
  • 无阻塞的等待:QtConcurrent提供了一种机制来等待并行任务的完成,而不会阻塞调用线程。
Header:
#include <QtConcurrent> 
qmake:
QT += concurrent

QtConcurrent基本用法:

  QtConcurrent::run这行代码的作用是启动ch72_concurrent类的timeTask成员函数在单独的线程中异步执行,并且返回一个QFuture<int>对象,用于跟踪执行的状态和获取返回值。

QFuture<int> ft = QtConcurrent::run(this, &ch72_concurrent::timeTask);while (!ft.isFinished())
{//用于处理事件队列中的事件的。这个函数可以确保应用程序界面保持响应状态,即使在执行长时间运行的任务时//当future未完成时,让cpu去做别的事QApplication::processEvents(QEventLoop::AllEvents, 30); 
}

示例:

xx.h

#pragma once#include <QtWidgets/QWidget>
#include "ui_ch72_concurrent.h"class ch72_concurrent : public QWidget
{Q_OBJECTpublic:ch72_concurrent(QWidget *parent = nullptr);~ch72_concurrent();int timeTask();private slots:void on_pushButton_clicked();private:Ui::ch72_concurrentClass ui;
};

xx.cpp

#include "ch72_concurrent.h"
#include <QThread>
#include <QDebug>
#include <QtConcurrent>
#include <QFuture>ch72_concurrent::ch72_concurrent(QWidget *parent): QWidget(parent)
{ui.setupUi(this);
}ch72_concurrent::~ch72_concurrent()
{}int ch72_concurrent::timeTask()
{int num = 0;for (int i = 0; i < 1000000; i++){num++;qDebug() << num;}return num;
}void ch72_concurrent::on_pushButton_clicked()
{//timeTask();QFuture<int> ft = QtConcurrent::run(this, &ch72_concurrent::timeTask);while (!ft.isFinished()){//用于处理事件队列中的事件的。这个函数可以确保应用程序界面保持响应状态,即使在执行长时间运行的任务时QApplication::processEvents(QEventLoop::AllEvents, 30); }
}

main.cpp

#include "ch72_concurrent.h"
#include <QtWidgets/QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);ch72_concurrent w;w.show();return a.exec();
}

三、QtConcurrent run参数说明

  QtConcurrent run函数参数,可以是全局函数,也可以是类成员函数。

  • 类成员函数做run参数

    int ch73_concurrent::timeTask(int num1, int num2)
    {//int num = 0;for (int i = 0; i < 1000000; i++){num1++;num2++;qDebug() << num1;qDebug() << num2;}return num1 + num2;
    }void ch73_concurrent::on_pushButton_clicked()
    {//timeTask();int num1 = 0;int num2 = 0;QFuture<int> ft = QtConcurrent::run(this, &ch73_concurrent::timeTask, num1, num2);while (!ft.isFinished()){QApplication::processEvents(QEventLoop::AllEvents, 30);}
    }
    
  • 全局函数做run参数

    static int gTimeTask(int num1, int num2)
    {//int num = 0;for (int i = 0; i < 1000000; i++){num1++;num2++;qDebug() << num1;qDebug() << num2;}return num1 + num2;
    }void ch73_concurrent::on_pushButton_clicked()
    {//timeTask();int num1 = 0;int num2 = 0;//QFuture<int> ft = QtConcurrent::run(this, &ch73_concurrent::timeTask, num1, num2);QFuture<int> ft = QtConcurrent::run(gTimeTask, num1, num2);while (!ft.isFinished()){QApplication::processEvents(QEventLoop::AllEvents, 30);}
    }
    

四、获取QtConcurrent的返回值

  获取QtConcurrent的结果,需要使用QFutureWatcher类,链接它的信号finished,然后给watcher设置future,当监控到future执行结束后,可以获取它的执行结果,调用的是result()函数。

int ch74::timeTask(int& num1, int& num2)
{for (int i = 0; i < 1000; i++){num1++;num2++;qDebug() << num1;qDebug() << num2;}return num1 + num2;
}void ch74::on_pushButton_clicked()
{int num1 = 0;int num2 = 0;QFutureWatcher<int>* fw = new QFutureWatcher<int>;connect(fw, &QFutureWatcher<int>::finished, [&]{qDebug() << "QFutureWatcher finished";qDebug() << "result = " << fw->result();});QFuture<int> ft = QtConcurrent::run(this, &ch74::timeTask, num1, num2);fw->setFuture(ft);while (!ft.isFinished()){QApplication::processEvents(QEventLoop::AllEvents, 30);}
}

五、C++其他线程技术介绍

  • pthread:linux线程
    • 这是POSIX线程库,主要用于Unix-like系统,如Linux和macOS。
    • 提供了丰富的线程创建和管理功能,包括互斥锁、条件变量等同步机制。
  • win32-pthread,obs的线程全部使用了win32-pthread
    • 这是一个在Windows平台上模拟POSIX线程库的库。
    • OBS(Open Broadcaster Software)等应用程序使用这个库来实现跨平台的线程功能。
  • windows thread类
    • Windows API提供了自己的线程类,如Win32 Thread,用于创建和管理线程。
    • 这些线程类通常与Windows特定的功能紧密结合,如窗口消息处理。
  • MFC thread类
    • MFC(Microsoft Foundation Classes)是一套C++库,用于Windows应用程序开发。
    • MFC提供了自己的线程类,例如CWinThread,用于简化线程的创建和管理。
  • boost
    • Boost库是一个广泛使用的C++库集合,其中包含了线程库。
    • Boost.Thread提供了跨平台的线程支持,包括线程创建、同步等。
  • std::thread(推荐使用这个,基于语言级别的跨平台C++线程)
    • C++11标准引入了std::thread,这是语言级别的线程支持。
    • 推荐使用std::thread,因为它提供了跨平台的线程支持,并且与C++标准库紧密集成。
    • std::thread简化了线程的创建和管理,同时提供了丰富的同步机制,如std::mutexstd::condition_variable等。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/42024.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

理解算法复杂度:空间复杂度详解

引言 在计算机科学中&#xff0c;算法复杂度是衡量算法效率的重要指标。时间复杂度和空间复杂度是算法复杂度的两个主要方面。在这篇博客中&#xff0c;我们将深入探讨空间复杂度&#xff0c;了解其定义、常见类型以及如何进行分析。空间复杂度是衡量算法在执行过程中所需内存…

ceph mgr [errno 39] RBD image has snapshots (error deleting image from trash)

ceph mgr 报错 debug 2024-07-08T09:25:56.512+0000 7f9c63bd2700 0 [rbd_support INFO root] execute_task: task={"sequence": 3, "id": "260b9fee-d567-4301-b7eb-b1fe1b037413", "message": "Removing image replicapool/8…

昇思25天学习打卡营第19天|Diffusion扩散模型

学AI还能赢奖品&#xff1f;每天30分钟&#xff0c;25天打通AI任督二脉 (qq.com) Diffusion扩散模型 本文基于Hugging Face&#xff1a;The Annotated Diffusion Model一文翻译迁移而来&#xff0c;同时参考了由浅入深了解Diffusion Model一文。 本教程在Jupyter Notebook上成…

python库 - missingno

missingno 是一个用于可视化和分析数据集中缺失值的 Python 库。它提供了一系列简单而强大的工具&#xff0c;帮助用户直观地理解数据中的缺失模式&#xff0c;从而更好地进行数据清洗和预处理。missingno 库特别适用于数据分析和数据科学项目&#xff0c;尤其是在处理缺失数据…

昇思MindSpore学习笔记5-02生成式--RNN实现情感分类

摘要&#xff1a; 记录MindSpore AI框架使用RNN网络对自然语言进行情感分类的过程、步骤和方法。 包括环境准备、下载数据集、数据集加载和预处理、构建模型、模型训练、模型测试等。 一、概念 情感分类。 RNN网络模型 实现效果&#xff1a; 输入: This film is terrible 正…

放大镜案例

放大镜 <!DOCTYPE html> <html lang"zh-cn"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>商品放大镜</title><link rel&qu…

如何使用allure生成测试报告

第一步下载安装JDK1.8&#xff0c;参考链接JDK1.8下载、安装和环境配置教程-CSDN博客 第二步配置allure环境&#xff0c;参考链接allure的安装和使用(windows环境)_allure windows-CSDN博客 第三步&#xff1a; 第四步&#xff1a; pytest 查看目前运行的测试用例有无错误 …

如何使用 pytorch 创建一个神经网络

我已发布在&#xff1a;如何使用 pytorch 创建一个神经网络 SapientialM.Github.io 构建神经网络 1 导入所需包 import os import torch from torch import nn from torch.utils.data import DataLoader from torchvision import datasets, transforms2 检查GPU是否可用 dev…

ffmpeg滤镜创建过程

1、创建一个滤镜图 AVFilterGraph *filter_graph avfilter_graph_alloc(); 2、创建滤镜的输入和输出 AVFilterInOut *inputs avfilter_inout_alloc(); AVFilterInOut *outputs avfilter_inout_alloc(); 3、每个滤镜创建上下文 AVFilterContext *filter1_ctx avfilter_…

Yolov10训练,转化onnx,推理

yolov10对于大目标的效果好&#xff0c;小目标不好 一、如果你训练过yolov5&#xff0c;yolov8&#xff0c;的话那么你可以直接用之前的环境就行 目录 一、如果你训练过yolov5&#xff0c;yolov8&#xff0c;的话那么你可以直接用之前的环境就行 二、配置好后就可以配置文件…

android webview 远程调试

打开远程调试选项 MainActivity super.onCreate(savedInstanceState);// enable Cordova apps to be started in the backgroundBundle extras getIntent().getExtras();if (extras ! null && extras.getBoolean("cdvStartInBackground", false)) {moveT…

前端JS特效第24集:jquery css3实现瀑布流照片墙特效

jquery css3实现瀑布流照片墙特效&#xff0c;先来看看效果&#xff1a; 部分核心的代码如下(全部代码在文章末尾)&#xff1a; <!DOCTYPE html> <html lang"en"> <head> <meta charset"UTF-8" /> <title>jquerycss3实现瀑…

Nginx:负载均衡小专题

运维专题 Nginx&#xff1a;负载均衡小专题 - 文章信息 - Author: 李俊才 (jcLee95) Visit me at CSDN: https://jclee95.blog.csdn.netMy WebSite&#xff1a;http://thispage.tech/Email: 291148484163.com. Shenzhen ChinaAddress of this article:https://blog.csdn.net/…

在Conda环境中高效使用Kubernetes:跨平台容器化实践指南

摘要 Conda 是一个流行的跨平台包和环境管理器&#xff0c;广泛用于Python社区。而 Kubernetes 是一个开源的容器编排系统&#xff0c;用于自动化部署、扩展和管理容器化应用程序。本文将探讨如何在 Conda 环境中使用 Kubernetes&#xff0c;包括设置 Conda 环境、容器化应用程…

【专项刷题】— 位运算

常见类型介绍&#xff1a; & &#xff1a;有 0 就是 0 | &#xff1a;有 1 就是 1 ^ &#xff1a;相同为 0 &#xff0c;相异为 1 或者 无进位相加给定一个数确定它的二进制位的第x个数是0还是1&#xff1a;将一个数的二进制的第x位改成1&#xff1a;将一个数的二进制的第x…

Windows10/11家庭版开启Hyper-V虚拟机功能详解

Hyper-V是微软的一款虚拟机软件&#xff0c;可以使我们在一台Windows PC上&#xff0c;在虚拟环境下同时运行多个互相之间完全隔离的操作系统&#xff0c;这就实现了在Windows环境下运行Linux以及其他OS的可能性。和第三方虚拟机软件&#xff0c;如VMware等相比&#xff0c;Hyp…

Linux应用编程IO基础

Linux应用编程基本IO操作 一、main 函数1、main 函数写法之无传参2、main 函数写法之有传参 二、open 打开文件三、write 写文件四、read 读文件五、close 关闭文件六、 lseek七、 返回错误处理与 errno7.1 strerror 函数7.2 perror 函数 八、 exit、_exit、_Exit8.1_exit()和_…

零基础自学爬虫技术该从哪里入手?

零基础学习Python并不一定是困难的&#xff0c;这主要取决于个人的学习方法、投入的时间以及学习目标的设定。Python是一门相对容易入门的编程语言&#xff0c;它有着简洁的语法、丰富的库和广泛的应用领域&#xff08;如数据分析、Web开发、人工智能等&#xff09;&#xff0c…

大模型知识问答: 文本分块要点总结

节前&#xff0c;我们组织了一场算法岗技术&面试讨论会&#xff0c;邀请了一些互联网大厂朋友、今年参加社招和校招面试的同学。 针对大模型技术趋势、算法项目落地经验分享、新手如何入门算法岗、该如何准备面试攻略、面试常考点等热门话题进行了深入的讨论。 总结链接如…

C++ 信号量和锁的区别

网上关于信号量和锁的区别&#xff0c;写的比较官方晦涩难懂&#xff0c;对于这个知识点吸收难&#xff0c;通过示例&#xff0c;我们看到信号量&#xff0c;可以控制同一时刻的线程数量&#xff0c;就算同时开启很多线程&#xff0c;依然可以的达到线程数可控 #include <i…