QT 线程暂停,继续执行的一种实现(有些道理,而且封装了)

注意:本次实现线程的暂停执行主要采用互斥量的方法,如果有更好的实现方法的小伙伴可以在下面留言!

直接插入代码了,由于做的小demo,代码写的可能有点乱,但还算完整。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QDebug>
#include <QThread>
#include <QMutex>
class MyThread:public QThread
{
    Q_OBJECT
public:
    MyThread();
    ~MyThread();
    void run();
public slots:
    void threadStart();
    void threadPause();
    void threadStop();
    void threadResume();
    void threadPR();
private:
    bool m_buttonState; //if pause m_buttonState=false;else m_buttonState=true;
    int m_i;
    QMutex m_mutex;//互斥量
};
 
#endif // MYTHREAD_H
复制代码
 1 //mythread.cpp
 2 #include "mythread.h"
 3 MyThread::MyThread()
 4 {
 5     m_i=0;
 6     m_buttonState=false;
 7 }
 8 
 9 MyThread::~MyThread()
10 {
11 
12 }
13 
14 void MyThread::run()
15 {
16     m_buttonState=true;
17     while(1)
18     {
19         m_mutex.lock();
20         m_i++;
21         qDebug()<<QString("the value of m_i is %1 ").arg(m_i);
22         m_mutex.unlock();
23         this->sleep(1);
24     }
25 
26 
27 
28 }
29 
30 
31 void MyThread::threadPause()
32 {
33     qDebug()<<QString("pause :%1").arg(m_buttonState);
34         this->m_mutex.lock();
35         this->m_buttonState=false;
36         qDebug()<<QString("pause");
37 }
38 void MyThread::threadResume()
39 {
40       qDebug()<<QString("resume :%1").arg(m_buttonState);
41         this->m_mutex.unlock();
42         this->m_buttonState=true;
43         qDebug()<<QString("resume");
44 
45 }
46 void MyThread::threadStop()
47 {
48     this->exit();
49 
50 }
51 void MyThread::threadStart()
52 {
53     this->start();
54 }
55 void MyThread::threadPR()
56 {
57     if(m_buttonState)
58     {
59         threadPause();
60 
61     }
62     else
63     {
64         threadResume();
65     }
66 
67 }
复制代码
复制代码
 1 //mainwindow.h
 2 #ifndef MAINWINDOW_H
 3 #define MAINWINDOW_H
 4 
 5 #include <QMainWindow>
 6 #include "mythread.h"
 7 namespace Ui {
 8 class MainWindow;
 9 }
10 
11 class MainWindow : public QMainWindow
12 {
13     Q_OBJECT
14 
15 public:
16     explicit MainWindow(QWidget *parent = 0);
17     ~MainWindow();
18 private slots:
19     void changeButton();
20     void threadStop();
21     void threadStart();
22     void threadPR();
23 private:
24     Ui::MainWindow *ui;
25     MyThread *myThread;
26     MyThread *oneThread;
27 
28 };
29 
30 #endif // MAINWINDOW_H
复制代码
复制代码
 1 //mainwindow.cpp
 2 #include "mainwindow.h"
 3 #include "ui_mainwindow.h"
 4 
 5 MainWindow::MainWindow(QWidget *parent) :
 6     QMainWindow(parent),
 7     ui(new Ui::MainWindow)
 8 {
 9     ui->setupUi(this);
10     myThread=new MyThread;
11     oneThread=new MyThread;
12     connect(ui->pauseButton,SIGNAL(clicked()),this,SLOT(changeButton()));
13     connect(ui->startButton,SIGNAL(clicked()),this,SLOT(threadStart()));
14     connect(ui->pauseButton,SIGNAL(clicked()),this,SLOT(threadPR()));
15     connect(ui->stopButton,SIGNAL(clicked()),this,SLOT(threadStop()));
16 }
17 
18 MainWindow::~MainWindow()
19 {
20     if(ui!=NULL)
21     {
22         delete ui;
23         ui=NULL;
24     }
25     if(myThread!=NULL)
26     {
27         delete myThread;
28         myThread=NULL;
29     }
30     if(oneThread!=NULL)
31     {
32         delete oneThread;
33         oneThread=NULL;
34     }
35 }
36 
37 void MainWindow::changeButton()
38 {
39 
40 
41     if(!QString::compare(ui->pauseButton->text(),QString::fromUtf8("暂停")))
42     {
43         ui->pauseButton->setText(QString::fromUtf8("继续"));
44     }else
45     {
46         ui->pauseButton->setText(QString::fromUtf8("暂停"));
47     }
48 }
49 
50 void MainWindow::threadStart()
51 {
52     myThread->threadStart();
53     oneThread->threadStart();
54 
55 }
56 void MainWindow::threadStop()
57 {
58     myThread->terminate();
59     oneThread->terminate();
60 
61 }
62 void MainWindow::threadPR()
63 {
64     myThread->threadPR();
65     oneThread->threadPR();
66 
67 
68 }
复制代码

还有一个简单的ui界面就不贴了,就三个button

暂停、继续就可以实现了,但很奇怪的事情是当将线程terminate 后再调用start 也会好像出现“暂停、继续”的效果,这个正在思考中,如果小伙伴有知道的留言tell me啊!

原文地址:https://www.cnblogs.com/findumars/p/5813292.html