QT线程之QMutex

浅浅的花香味﹌ 2022-03-18 15:29 415阅读 0赞

所有文件
在这里插入图片描述
dialog.h文件

  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3. #include <QDialog>
  4. #include "qdicethread.h"
  5. #include <QTimer>
  6. namespace Ui {
  7. class Dialog;
  8. }
  9. class Dialog : public QDialog
  10. {
  11. Q_OBJECT
  12. private:
  13. QDiceThread threadA;
  14. int m_seq,m_diceValue;
  15. QTimer m_timer;
  16. protected:
  17. void closeEvent(QCloseEvent *event);
  18. public:
  19. explicit Dialog(QWidget *parent = 0);
  20. ~Dialog();
  21. private slots:
  22. void onThreadA_start();
  23. void onThreadA_finish();
  24. void onTimerOut();
  25. void on_pushButton_clicked();
  26. void on_pushButton_4_clicked();
  27. void on_pushButton_2_clicked();
  28. void on_pushButton_3_clicked();
  29. void on_pushButton_5_clicked();
  30. private:
  31. Ui::Dialog *ui;
  32. };
  33. #endif // DIALOG_H

qdicethread.h文件

  1. #ifndef QDICETHREAD_H
  2. #define QDICETHREAD_H
  3. #include <QThread>
  4. #include <QMutex>
  5. class QDiceThread : public QThread
  6. {
  7. public:
  8. QDiceThread();
  9. private:
  10. QMutex mutex;
  11. int m_seq=0;
  12. int m_diceValue;
  13. bool m_paused=true;
  14. bool m_stop=false;
  15. protected:
  16. void run()Q_DECL_OVERRIDE;
  17. public:
  18. void diceBegin();
  19. void diceEnd();
  20. void stopThread();
  21. bool readValue(int *seq,int *diceValue);
  22. };
  23. #endif // QDICETHREAD_H

dialog.cpp文件

  1. #include "dialog.h"
  2. #include "ui_dialog.h"
  3. Dialog::Dialog(QWidget *parent) :
  4. QDialog(parent),
  5. ui(new Ui::Dialog)
  6. {
  7. ui->setupUi(this);
  8. connect(&threadA,SIGNAL(started()),this,SLOT(onThreadA_start()));
  9. connect(&threadA,SIGNAL(finished()),this,SLOT(onThreadA_finish()));
  10. connect(&m_timer,SIGNAL(timeout()),this,SLOT(onTimerOut()));
  11. }
  12. Dialog::~Dialog()
  13. {
  14. delete ui;
  15. }
  16. void Dialog::onThreadA_start()
  17. {
  18. ui->label_2->setText("Thread状态:开启");
  19. }
  20. void Dialog::onThreadA_finish()
  21. {
  22. ui->label_2->setText("Thread状态:关闭");
  23. }
  24. void Dialog::onTimerOut()
  25. {
  26. int tmpSeq=0,tmpDiceValue=0;
  27. bool valid=threadA.readValue(&tmpSeq,&tmpDiceValue);
  28. if(valid && (tmpSeq!=m_seq))
  29. {
  30. m_seq=tmpSeq;
  31. m_diceValue=tmpDiceValue;
  32. QString str=QString::asprintf("第%d次掷色子,点数为%d",m_seq,m_diceValue);
  33. ui->plainTextEdit->appendPlainText(str);
  34. QPixmap pic;
  35. QString fileName=QString::asprintf(":/dice/images/d%d.jpg",m_diceValue);
  36. pic.load(fileName);
  37. ui->label->setPixmap(pic);
  38. }
  39. }
  40. void Dialog::on_pushButton_clicked()
  41. {
  42. threadA.start();
  43. m_seq=0;
  44. }
  45. void Dialog::on_pushButton_4_clicked()
  46. {
  47. threadA.stopThread();
  48. threadA.wait();
  49. }
  50. void Dialog::on_pushButton_2_clicked()
  51. {
  52. threadA.diceBegin();
  53. m_timer.start(100);
  54. }
  55. void Dialog::on_pushButton_3_clicked()
  56. {
  57. threadA.diceEnd();
  58. m_timer.stop();
  59. }
  60. void Dialog::on_pushButton_5_clicked()
  61. {
  62. ui->plainTextEdit->clear();
  63. }
  64. void Dialog::closeEvent(QCloseEvent *event)
  65. {
  66. if(threadA.isRunning())
  67. {
  68. threadA.stopThread();
  69. threadA.wait();
  70. }
  71. event->accept();
  72. }

qdicethread.cpp文件

  1. #include "qdicethread.h"
  2. #include <QTime>
  3. QDiceThread::QDiceThread()
  4. {
  5. }
  6. void QDiceThread::run()
  7. {
  8. m_stop=false;
  9. m_seq=0;
  10. qsrand(QTime::currentTime().msec());
  11. while(!m_stop)
  12. {
  13. if(!m_paused)
  14. {
  15. mutex.lock();
  16. m_seq++;
  17. m_diceValue=qrand();
  18. m_diceValue=m_diceValue%6+1;
  19. mutex.unlock();
  20. }
  21. msleep(500);
  22. }
  23. }
  24. bool QDiceThread::readValue(int *seq, int *diceValue)
  25. {
  26. if(mutex.tryLock())
  27. {
  28. *seq=m_seq;
  29. *diceValue=m_diceValue;
  30. mutex.unlock();
  31. return true;
  32. }
  33. else
  34. return false;
  35. }
  36. void QDiceThread::diceBegin()
  37. {
  38. m_paused=false;
  39. }
  40. void QDiceThread::diceEnd()
  41. {
  42. m_paused=true;
  43. }
  44. void QDiceThread::stopThread()
  45. {
  46. m_stop=true;
  47. }

运行结果

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,415人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Qt线

    今天学习Qt的多线程,在学习多线程主要是两个方面。一是多线程的基础概念,二是多线程的同步,三是怎么和主线程进行通讯。 三.Qt 的应用程序开始执行的时候,只有一个主线程在运行

    相关 Qt: QMutex

    QMutex QMutex提供了线程间的顺序访问。 QMutex的目的是保护一个对象、数据结构或者代码段,所以同一时间只有一个线程可以访问它。(类似java的"sync