qt 线程的创建和退出

qt常用的线程创建有两种方法,一种是继承QThread,重写run函数,一种是创建一个业务处理类,moveToThread。

我创建了一个widget工程,演示一下。

示例地址:https://files-cdn.cnblogs.com/files/warmlight/TestThread20220923.rar?t=1663900098

MainWindow.h:

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include "thread1work.h"
 6 #include "thread2work.h"
 7 
 8 namespace Ui {
 9 class MainWindow;
10 }
11 
12 class MainWindow : public QMainWindow
13 {
14     Q_OBJECT
15 
16 public:
17     explicit MainWindow(QWidget *parent = nullptr);
18     ~MainWindow();
19 
20 private slots:
21     void on_btnStartThread1_clicked();
22 
23     void on_btnStartThread2_clicked();
24 
25 private:
26     Ui::MainWindow *ui;
27 
28     QThread* m_thread1;
29     //QThread* m_thread2;
30     Thread1Work* m_thread1Work;
31     Thread2Work* m_thread2Work;
32 };
33 
34 #endif // MAINWINDOW_H

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 #include <QDateTime>
 4 #include <QDebug>
 5 
 6 MainWindow::MainWindow(QWidget *parent) :
 7     QMainWindow(parent),
 8     ui(new Ui::MainWindow)
 9 {
10     ui->setupUi(this);
11 }
12 
13 MainWindow::~MainWindow()
14 {
15     delete ui;
16 }
17 
18 void MainWindow::on_btnStartThread1_clicked()
19 {
20     if(ui->btnStartThread1->isChecked()){
21         ui->btnStartThread1->setText("停止线程1");
22         m_thread1Work = new Thread1Work();
23         m_thread1 = new QThread();
24         m_thread1Work->moveToThread(m_thread1);
25         connect(m_thread1, &QThread::started, m_thread1Work, &Thread1Work::DoWork);//线程开始执行业务处理
26         connect(m_thread1, &QThread::finished, m_thread1Work, &QObject::deleteLater);//线程结束释放工作类
27         connect(m_thread1, &QThread::finished, m_thread1, &QObject::deleteLater);//线程结束释放线程指针
28         m_thread1->start();
29     }
30     else {
31         ui->btnStartThread1->setText("启动线程1");
32         m_thread1Work->StopThread();
33 
34         m_thread1->exit(0);//退出线程
35         QDateTime startTime = QDateTime::currentDateTime();
36         m_thread1->wait();//10秒 wait的参数是microsecond,即微妙
37         QDateTime endTime = QDateTime::currentDateTime();
38         qint64 intervalMs = startTime.msecsTo(endTime);
39         qDebug() << intervalMs;
40         m_thread1 = nullptr;
41         m_thread1Work = nullptr;
42     }
43 
44 }
45 
46 void MainWindow::on_btnStartThread2_clicked()
47 {
48     if(ui->btnStartThread2->isChecked()){
49         ui->btnStartThread2->setText("停止线程2");
50         m_thread2Work = new Thread2Work();
51 
52         connect(m_thread2Work, &QThread::finished, m_thread2Work, &QObject::deleteLater);//线程结束释放线程类
53         m_thread2Work->start();
54     }
55     else {
56         ui->btnStartThread2->setText("启动线程2");
57         m_thread2Work->StopThread();
58 
59         m_thread2Work->quit();//退出线程
60         QDateTime startTime = QDateTime::currentDateTime();
61         m_thread2Work->wait();//等待线程退出
62         QDateTime endTime = QDateTime::currentDateTime();
63         qint64 intervalMs = startTime.msecsTo(endTime);
64         qDebug() << intervalMs;
65         m_thread2Work = nullptr;
66     }
67 }
Thread1Work.h
 1 #ifndef THREAD1_H
 2 #define THREAD1_H
 3 
 4 #include <QObject>
 5 #include <QThread>
 6 
 7 class Thread1Work : public QObject
 8 {
 9     Q_OBJECT
10 public:
11     explicit Thread1Work(QObject *parent = nullptr);
12 
13 public slots:
14     void StopThread();
15     void DoWork();
16 signals:
17 
18 
19 private:
20     bool m_bStop;
21     int m_nCount;
22 };
23 
24 #endif // THREAD1_H

 

Thread1Work.cpp
 1 #include "thread1work.h"
 2 #include <QDebug>
 3 
 4 Thread1Work::Thread1Work(QObject *parent) : QObject(parent)
 5 {
 6     m_bStop = false;
 7     m_nCount = 0;
 8 }
 9 
10 void Thread1Work::StopThread()
11 {
12     m_bStop = true;
13 }
14 
15 void Thread1Work::DoWork()
16 {
17 
18     while (!m_bStop) {
19         char* p = new char[5000000];
20         qInfo() << "Thread1---Thread-ID: " << QThread::currentThreadId() << " Count: " << m_nCount++;
21         QThread::msleep(100);
22         delete[] p;
23         p = nullptr;
24 
25     }
26     qInfo() << "Thread1---Thread-ID: " << QThread::currentThreadId() << " Exit";
27 }

 

Thread2Work.h
 1 #ifndef THREAD2_H
 2 #define THREAD2_H
 3 
 4 #include <QThread>
 5 
 6 class Thread2Work : public QThread
 7 {
 8 public:
 9     Thread2Work();
10     void StopThread();
11 protected:
12     void run() override;
13 
14 private:
15     bool m_bStop;
16     int m_nCount;
17 };
18 
19 #endif // THREAD2_H

 

Thread2Work.cpp
 1 #include "thread2work.h"
 2 #include <QDebug>
 3 
 4 Thread2Work::Thread2Work()
 5 {
 6     m_bStop = false;
 7     m_nCount = 0;
 8 }
 9 
10 void Thread2Work::StopThread()
11 {
12     m_bStop = true;
13 }
14 
15 void Thread2Work::run()
16 {
17     while (!m_bStop) {
18         qInfo() << "Thread2---Thread-ID: " << QThread::currentThreadId() << " Count: " << m_nCount++;
19         QThread::msleep(100);
20     }
21     qInfo() << "Thread2---Thread-ID: " << QThread::currentThreadId() << " Exit";
22 }

 

posted @ 2022-09-23 10:27  阳光下的小土豆  阅读(812)  评论(0编辑  收藏  举报