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();
}