当前位置: 首页 > news >正文

Qt - 进程/线程 补充进阶

Qt - 进程/线程 补充进阶

  • 多线程
    • quit / eixt / terminate
  • QThread例子
    • tdicethread 类
      • .h
      • .cpp
    • widget 类
      • .h
      • .cpp
  • 线程同步

多线程

quit / eixt / terminate

quit

应用程序或线程安全的取消事件处理队列的执行,并随后使线程退出(如果只希望结束线程并保证它安全的清理资源,最好使用quit)
当调用quit()方法时,线程会等待一段时间来完成当前正在运行的任务和冲洗缓存,然后终止线程并发出finished()信号,告诉其他对象该线程已终止
如果线程正在执行事件循环,则该函数会等待事件循环结束后退出线程(如果线程没有运行时间循环,则该函数不会有任何效果)

eixt

直接停止线程,未处理完的事件将被丢弃并静默忽略
该方法没有给予线程任何机会来清理自己的资源和数据,并可能导致应用程序的崩溃(尽量避免使用此方法)
该方法不管线程是否在事件循环中运行,会发送一个finished信号以通知线程已经退出

terminate

可以强制终止一个线程,但是相比exit更加暴力
该方法会强制关闭线程并释放正在使用的所有资源,可以在某些情况下正常处理一个卡住的线程,但也可能导致数据丢失、资源泄漏以及应用程序不稳定(尽量避免使用该方法)

QThread例子

掷骰子

在这里插入图片描述
在这里插入图片描述

tdicethread 类

.h

#ifndef TDICETHREAD_H
#define TDICETHREAD_H#include <QObject>
#include <QThread>
#include <QRandomGenerator>class TDiceThread : public QThread
{Q_OBJECT
public:explicit TDiceThread(QObject *parent = nullptr);void diceBegin(); // 开始投骰子void dicePause(); // 暂停投void stopThread(); //  结束线程signals:void newValue(int seq,int diceValue); // 产生新点数的信号private:int m_seq=0; //int m_diceValue; // 骰子点数bool m_paused=true; // 暂停投骰子bool m_stop=false; // 停止线程 退出runprotected:void run();
};#endif // TDICETHREAD_H

.cpp

#include "tdicethread.h"TDiceThread::TDiceThread(QObject *parent): QThread{parent}
{}void TDiceThread::diceBegin() // 开始投骰子
{m_paused=false;
}void TDiceThread::dicePause() // 暂停投
{m_paused=true;
}void TDiceThread::stopThread() //  结束线程
{m_stop=true;
}void TDiceThread::run()
{m_stop=false;m_paused=true;while (!m_stop){if (!m_paused){m_diceValue=QRandomGenerator::global()->bounded(1,7); // 生成随机数m_seq++;emit newValue(m_seq,m_diceValue); //发送信号}msleep(500); // 线程休眠500ms}quit(); // m_stop==true时结束线程任务 等价于exit(0)
}

widget 类

.h

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QCloseEvent>
#include "tdicethread.h"QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private:Ui::Widget *ui;TDiceThread *threadA; // 工作线程protected:void closeEvent(QCloseEvent *event);private slots:void threadA_started();void threadA_finished();void threadA_newValue(int seq,int diceValue);void on_pushButton_clicked();void on_pushButton_2_clicked();void on_pushButton_3_clicked();void on_pushButton_4_clicked();
};
#endif // WIDGET_H

.cpp

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);threadA=new TDiceThread(this);connect(threadA,&TDiceThread::started,this,&Widget::threadA_started); // started信号继承自QThreadconnect(threadA,&TDiceThread::finished,this,&Widget::threadA_finished); // finished信号继承自QThreadconnect(threadA,&TDiceThread::newValue,this,&Widget::threadA_newValue); // 通过信号和槽传递参数}Widget::~Widget()
{delete ui;
}void Widget::threadA_started()
{qDebug()<<"====== threadA started ======";
}
void Widget::threadA_finished()
{qDebug()<<"====== threadA finished ======";
}void Widget::threadA_newValue(int seq,int diceValue)
{QString str=QString::asprintf("the %d times, point num is:%d",seq,diceValue);qDebug()<<str;
}void Widget::closeEvent(QCloseEvent *event)
{qDebug()<<"try to close main window";if (threadA->isRunning()){threadA->terminate(); // 强制终止线程threadA->wait(); // 等待子线程结束完成,主线程再执行}event->accept();
}void Widget::on_pushButton_clicked()
{qDebug()<<"start threadA";threadA->start();
}void Widget::on_pushButton_2_clicked()
{qDebug()<<"quit threadA";threadA->stopThread(); // 内部自动调用quit()
}void Widget::on_pushButton_3_clicked()
{qDebug()<<"start task";threadA->diceBegin();
}void Widget::on_pushButton_4_clicked()
{qDebug()<<"pause task";threadA->dicePause();
}

线程同步

http://www.lryc.cn/news/69391.html

相关文章:

  • spring笔记
  • 最大熵模型
  • 微服务中网关的配置
  • Linux基本指令实现4及热键指令详解
  • 系统调用与API
  • OpenPCDet系列 | 5.4.1 DenseHead中的AnchorGenerator锚框生成模块
  • 【开发者指南】如何在MyEclipse中使用HTML或JSP设计器?(上)
  • Node开发Web后台服务
  • Linux下对mmap封装使用
  • 深入了解云计算:发展历程、服务与部署模型、未来趋势与挑战
  • 使用乐鑫 Web IDE 助力物联网开发
  • Maven(5)---Maven的部署和发布
  • 内网渗透之权限维持-黄金白银票据隐藏账户远控-RustDeskGotoHTTP
  • 动态规划——带权活动选择
  • 软考A计划-真题-分类精讲汇总-第十八章(面向对象程序设计)
  • 【C++ 入坑指南】(09)数组
  • Vue.js
  • 博士毕业答辩流程 注意事项
  • 拼多多开放平台订单详情接口解析
  • 如何把ipa文件(iOS安装包)安装到iPhone手机上? 附方法汇总
  • 由浅入深了解 深度神经网络优化算法
  • LIN-报文结构
  • 南京邮电大学通达学院2023c++实验报告(三)
  • ISO9000和ISO9001有哪些区别?
  • 第7章异常、断言和曰志
  • springboot读取和写入csv文件数据
  • 【产品经理】工作交接
  • Springer期刊 latex投稿经验分享
  • Python 文件读取的练习
  • Redis:主从复制_通过此功能实现对内存上的数据更好的保护