QT线程之QMutex

所有文件
QT线程之QMutex_第1张图片
dialog.h文件

#ifndef DIALOG_H
#define DIALOG_H


#include 
#include "qdicethread.h"
#include 
namespace Ui {
class Dialog;
}


class Dialog : public QDialog
{
    Q_OBJECT
private:
    QDiceThread threadA;
    int m_seq,m_diceValue;
    QTimer m_timer;
protected:
    void closeEvent(QCloseEvent *event);
public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();


private slots:
    void onThreadA_start();
    void onThreadA_finish();
    void onTimerOut();
    void on_pushButton_clicked();


    void on_pushButton_4_clicked();


    void on_pushButton_2_clicked();


    void on_pushButton_3_clicked();


    void on_pushButton_5_clicked();


private:
    Ui::Dialog *ui;
};


#endif // DIALOG_H


qdicethread.h文件

#ifndef QDICETHREAD_H
#define QDICETHREAD_H


#include 
#include 
class QDiceThread : public QThread
{
public:
    QDiceThread();
private:
    QMutex mutex;
    int m_seq=0;
    int m_diceValue;
    bool m_paused=true;
    bool m_stop=false;
protected:
    void run()Q_DECL_OVERRIDE;
public:
    void diceBegin();
    void diceEnd();
    void stopThread();
    bool readValue(int *seq,int *diceValue);
};


#endif // QDICETHREAD_H


dialog.cpp文件

#include "dialog.h"
#include "ui_dialog.h"


Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    connect(&threadA,SIGNAL(started()),this,SLOT(onThreadA_start()));
    connect(&threadA,SIGNAL(finished()),this,SLOT(onThreadA_finish()));
    connect(&m_timer,SIGNAL(timeout()),this,SLOT(onTimerOut()));
}


Dialog::~Dialog()
{
    delete ui;
}
void Dialog::onThreadA_start()
{
    ui->label_2->setText("Thread状态:开启");
}
void Dialog::onThreadA_finish()
{
    ui->label_2->setText("Thread状态:关闭");
}
void Dialog::onTimerOut()
{
    int tmpSeq=0,tmpDiceValue=0;
    bool valid=threadA.readValue(&tmpSeq,&tmpDiceValue);
    if(valid && (tmpSeq!=m_seq))
    {
        m_seq=tmpSeq;
        m_diceValue=tmpDiceValue;
        QString str=QString::asprintf("第%d次掷色子,点数为%d",m_seq,m_diceValue);
        ui->plainTextEdit->appendPlainText(str);
        QPixmap pic;
        QString fileName=QString::asprintf(":/dice/images/d%d.jpg",m_diceValue);
        pic.load(fileName);
        ui->label->setPixmap(pic);
    }
}


void Dialog::on_pushButton_clicked()
{
    threadA.start();
    m_seq=0;
}




void Dialog::on_pushButton_4_clicked()
{
    threadA.stopThread();
    threadA.wait();
}


void Dialog::on_pushButton_2_clicked()
{
    threadA.diceBegin();
    m_timer.start(100);
}


void Dialog::on_pushButton_3_clicked()
{
    threadA.diceEnd();
    m_timer.stop();
}


void Dialog::on_pushButton_5_clicked()
{
    ui->plainTextEdit->clear();
}
void Dialog::closeEvent(QCloseEvent *event)
{
    if(threadA.isRunning())
    {
        threadA.stopThread();
        threadA.wait();
    }
    event->accept();
}


qdicethread.cpp文件

#include "qdicethread.h"
#include 
QDiceThread::QDiceThread()
{


}
void QDiceThread::run()
{
    m_stop=false;
    m_seq=0;
    qsrand(QTime::currentTime().msec());
    while(!m_stop)
    {
        if(!m_paused)
        {
            mutex.lock();
            m_seq++;
            m_diceValue=qrand();
            m_diceValue=m_diceValue%6+1;
            mutex.unlock();
        }
        msleep(500);
    }
}
bool QDiceThread::readValue(int *seq, int *diceValue)
{
    if(mutex.tryLock())
    {
        *seq=m_seq;
        *diceValue=m_diceValue;
        mutex.unlock();
        return true;
    }
    else
        return false;
}
void QDiceThread::diceBegin()
{
    m_paused=false;
}
void QDiceThread::diceEnd()
{
    m_paused=true;
}
void QDiceThread::stopThread()
{
    m_stop=true;
}


运行结果

QT线程之QMutex_第2张图片

你可能感兴趣的:(QT线程之QMutex)