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

直接插入代码了,由于做的小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啊!

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

  1. C++11消息队列 + Qt线程池 + QRunnable执行任务简单模型

    1.模板类queue,包含头文件<queue>中,是一个FIFO队列. queue.push():在队列尾巴增加数据 queue.pop():移除队列头部数据 queue.font():获 ...

  2. Qt中暂停线程的执行(主线程和工作线程共用一把锁,一旦主线程将它锁上,工作线程就无法运行了,这也是一个办法)

    在线程中定义一个信号量: QMutex pause; 把run()函数中循环执行的部分用信号量pause锁住:   void run()   {   while(1)   {   pause.lock ...

  3. Qt 线程基础(Thread Basics的翻译,线程的五种使用情况)

    Qt 线程基础(QThread.QtConcurrent等) 转载自:http://blog.csdn.net/dbzhang800/article/details/6554104 昨晚看Qt的Man ...

  4. 三个线程T1,T2,T3.保证顺序执行的三种方法

    经常看见面试题:有三个线程T1,T2,T3,有什么方法可以确保它们按顺序执行.今天手写测试了一下,下面贴出目前想到的3种实现方式 说明:这里在线程中我都用到了sleep方法,目的是更容易发现问题.之前 ...

  5. Qt 控制线程的顺序执行(使用QWaitCondition,并且线程类的run函数里记得加exec(),使得线程常驻)

    背景项目中用到多线程,对线程的执行顺序有要求: A.一个线程先收数据 B.一个线程处理数据 C.一个线程再将处理后的数据发送出去 要求三个线程按照ABC的顺序循环执行. 思路子类化多线程方法 重写子类 ...

  6. Thread类的sleep()方法和对象的wait()方法都可以让线程暂停执行,它们有什么区别? 线程的sleep()方法和yield()方法有什么区别?

    Thread类的sleep()方法和对象的wait()方法都可以让线程暂停执行,它们有什么区别? sleep()方法(休眠)是线程类(Thread)的静态方法,调用此方法会让当前线程暂停执行指定的时间 ...

  7. Qt线程(1) moveToThread

    若在Qt准备使用线程类一般有两种方式(1) 采用WorkObject配合QThread进行使用 (2)继承QThread, 重载run()函数即可. 注:采用Qt::Concurrent之类的不在本文 ...

  8. QT核心编程之Qt线程 (c)

    QT核心编程之Qt线程是本节要介绍的内容,QT核心编程我们要分几个部分来介绍,想参考更多内容,请看末尾的编辑推荐进行详细阅读,先来看本篇内容. Qt对线程提供了支持,它引入了一些基本与平台无关的线程类 ...

  9. Qt 学习之路 :Qt 线程相关类

    希望上一章有关事件循环的内容还没有把你绕晕.本章将重新回到有关线程的相关内容上面来.在前面的章节我们了解了有关QThread类的简单使用.不过,Qt 提供的有关线程的类可不那么简单,否则的话我们也没必 ...

随机推荐

  1. MySQL的优化点总结---通过计算多种状态的百分比看MySQL的性能情况

    1 读写比例: show global status like 'com_select';  获得服务器启动到目前查询操作执行的次数:show global status like 'com_inse ...

  2. 使用blktrace统计磁盘块I/O访问频率 + IO调度CFQ

    http://blog.chinaunix.net/uid-24774106-id-4096470.html http://blog.csdn.net/wyzxg/article/details/74 ...

  3. notification.setLatestEventInfo(context, title, message, pendingIntent); undefined

    notification.setLatestEventInfo(context, title, message, pendingIntent);    在target为23时删除了该方法,我们应该使用 ...

  4. java格式处理工具类

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...

  5. Java基础知识强化之集合框架笔记37:用户登录注册案例

    1. 登录注册案例分析图解: 2. 用户登录案例 详细分析 和 分包实现: (1)用户登录案例详细分析(面向对象思想) 按照如下的操作,可以让我们更符合面向对象思想: • 有哪些类呢?         ...

  6. Java基础知识强化09:String、StringBuffer和StringBuilder使用

    1. 对于三者使用的总结: (1).如果要操作少量的数据用 = String               (2).单线程操作字符串缓冲区下操作大量数据 = StringBuilder (3).多线程操 ...

  7. easyui总结

    1.设置一个区域可拖动的第一种方法直接用html<div id="dd" style="width:100px;height:100px;">< ...

  8. AIX系统上压缩与解压文件

    压缩. 命令格式: #tar -cvf (或xvf)+文件名+设备 C:是本地到其他设备 x:是其他设备到本地 r:是追加,比如打包时,将其他文件追加进来使用该参数. t:显示tar包里的内容,但还原 ...

  9. ORACLE用户管理方式下备份数据和复制数据库

    首先要明确的是,oracle数据库的备份可以分为逻辑备份和物理备份.           逻辑备份的是通过数据导出对数据进行备份,主要方式有老式的IMP/EXP和数据泵灯方式.适合变化较少的数据库,而 ...

  10. npm的镜像替换成淘宝

    1.得到原本的镜像地址 npm get registry > https://registry.npmjs.org/ 设成淘宝的 npm config set registry http://r ...