网站建设技术思维导图,引流推广是什么,景点网站建设方案,上海建筑建材业招标公告Qt - 进程/线程 补充进阶 多线程quit / eixt / terminate QThread例子tdicethread 类.h.cpp widget 类.h.cpp 线程同步 多线程
quit / eixt / terminate
quit 应用程序或线程安全的取消事件处理队列的执行#xff0c;并随后使线程退出#xff08;如果只希望结束线程并保证它… 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 QRandomGeneratorclass 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_seq0; //int m_diceValue; // 骰子点数bool m_pausedtrue; // 暂停投骰子bool m_stopfalse; // 停止线程 退出runprotected:void run();
};#endif // TDICETHREAD_H
.cpp
#include tdicethread.hTDiceThread::TDiceThread(QObject *parent): QThread{parent}
{}void TDiceThread::diceBegin() // 开始投骰子
{m_pausedfalse;
}void TDiceThread::dicePause() // 暂停投
{m_pausedtrue;
}void TDiceThread::stopThread() // 结束线程
{m_stoptrue;
}void TDiceThread::run()
{m_stopfalse;m_pausedtrue;while (!m_stop){if (!m_paused){m_diceValueQRandomGenerator::global()-bounded(1,7); // 生成随机数m_seq;emit newValue(m_seq,m_diceValue); //发送信号}msleep(500); // 线程休眠500ms}quit(); // m_stoptrue时结束线程任务 等价于exit(0)
}
widget 类
.h
#ifndef WIDGET_H
#define WIDGET_H#include QWidget
#include QCloseEvent
#include tdicethread.hQT_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.hWidget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui-setupUi(this);threadAnew 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 strQString::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();
}
线程同步