欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

嵌入式--QT]QThread 创建多线程代码示例

最编程 2024-07-10 18:04:38
...

QDiceThread.h

#ifndef QDICETHREAD_H
#define QDICETHREAD_H

#include <QObject>
#include <QThread>
#include <QMutex>
class QDiceThread : public QThread
{
    Q_OBJECT

public:
    QDiceThread();
    ~QDiceThread();

    void run() Q_DECL_OVERRIDE;

    void diceBegin();//掷一次骰子
    void dicePause();//暂停
    void stopThread();//结束线程

    bool readValue(int* seq, int* diceValue);

signals:
    void newValue(int seq, int diceValue);//产生新点数的信号


private :
    int m_seq = 0;//掷骰子次数序号
    int m_diceValue;//骰子点数
    bool m_paused = true;//暂停
    bool m_stop = false;//停止

    QMutex mutex;

};

#endif // QDICETHREAD_H

QDiceThread.cpp

#include "QDiceThread.h"
#include <QTime>
#include <QMutexLocker>
QDiceThread::QDiceThread() {}

QDiceThread::~QDiceThread()
{
}

void QDiceThread::run()
{
    m_stop = false;
    m_seq = 0;
    qsrand(QTime::currentTime().msec());
    while(!m_stop) {
        if(!m_paused) {
            //QMutexLocker用法
            // QMutexLocker Locker(&mutex);
            //mutex用法
            mutex.lock();
            m_diceValue = qrand();
            m_diceValue = (m_diceValue % 6)+1;
            m_seq++;
            //使用互斥量读取数据,暂时注释掉信号操作
            // emit newValue(m_seq, m_diceValue);
            //mutex用法
            mutex.unlock();
        }
        msleep(500);
    }
    quit();
}

void QDiceThread::diceBegin()
{
    m_paused = false;
}

void QDiceThread::dicePause()
{
    m_paused = true;
}

void QDiceThread::stopThread()
{
    m_stop = true;
}

bool QDiceThread::readValue(int* seq, int* diceValue)
{
    if(mutex.tryLock()) {
        *seq = m_seq;
        *diceValue = m_diceValue;
        mutex.unlock();
        return true;
    } else {
        return false;
    }
}

QThreadDialog.h

#ifndef QTHREADDIALOG_H
#define QTHREADDIALOG_H

#include <QDialog>
#include "QDiceThread.h"
#include <QTimer>
namespace Ui
{
    class QThreadDialog;
}

class QThreadDialog : public QDialog
{
    Q_OBJECT

public:
    explicit QThreadDialog(QWidget* parent = nullptr);
    ~QThreadDialog();

    void closeEvent(QCloseEvent* event);

private slots:
    void on_pushButtonStart_clicked();

    void on_pushButtonPause_clicked();

    void on_pushButtonBegin_clicked();

    void on_pushButtonStop_clicked();

    void on_pushButtonClear_clicked();

public slots:
    void threadStatusStart();

    void threadStatusFinished();

    void threadNewValue(int seq, int diceValue);

    void onTimeOut();

private:
    Ui::QThreadDialog* ui;
    QDiceThread thread;

    QTimer timer;

    int mSeq, mDiceValue;
};

#endif // QTHREADDIALOG_H

QThreadDialog.cpp

#include "QThreadDialog.h"
#include "ui_QThreadDialog.h"

QThreadDialog::QThreadDialog(QWidget* parent)
    : QDialog(parent)
    , ui(new Ui::QThreadDialog)
{
    ui->setupUi(this);
    connect(&thread, SIGNAL(started()), this, SLOT(threadStatusStart()));
    connect(&thread, SIGNAL(finished()), this, SLOT(threadStatusFinished()));
    connect(&thread, &QDiceThread::newValue, this, &QThreadDialog::threadNewValue);
    connect(&timer, SIGNAL(timeout()), this, SLOT(onTimeOut()));
}

QThreadDialog::~QThreadDialog()
{
    delete ui;
}

void QThreadDialog::closeEvent(QCloseEvent* event)
{
    if(thread.isRunning()) {
        thread.stopThread();
        thread.wait();
    }
    event->accept();
}

void QThreadDialog::on_pushButtonStart_clicked()
{
    thread.start();
    mSeq = 0;
    ui->pushButtonStart->setEnabled(false);
    ui->pushButtonStop->setEnabled(true);
    ui->pushButtonBegin->setEnabled(true);
    ui->pushButtonPause->setEnabled(false);
}

void QThreadDialog::on_pushButtonStop_clicked()
{
    thread.stopThread();
    thread.wait();
    ui->pushButtonStart->setEnabled(true);
    ui->pushButtonStop->setEnabled(false);
    ui->pushButtonBegin->setEnabled(false);
    ui->pushButtonPause->setEnabled(false);
}


void QThreadDialog::on_pushButtonBegin_clicked()
{
    thread.diceBegin();
    timer.start(100);
    ui->pushButtonBegin->setEnabled(false);
    ui->pushButtonPause->setEnabled(true);
}

void QThreadDialog::on_pushButtonPause_clicked()
{
    thread.dicePause();
    timer.stop();
    ui->pushButtonBegin->setEnabled(true);
    ui->pushButtonPause->setEnabled(false);
}



void QThreadDialog::on_pushButtonClear_clicked()
{
    ui->plainTextEdit->clear();
}

void QThreadDialog::threadStatusStart()
{
    ui->labelStatus->setText("thread started");
}

void QThreadDialog::threadStatusFinished()
{
    ui->labelStatus->setText("thread finished");
}

void QThreadDialog::threadNewValue(int seq, int diceValue)
{
    QString str = QString::asprintf(u8"第%d次投掷骰子,点数为:%d", seq, diceValue);
    ui->plainTextEdit->appendPlainText(str);
}

void QThreadDialog::onTimeOut()
{
    int tmpSeq =0, tmpValue = 0;
    bool valid = thread.readValue(&tmpSeq, &tmpValue);
    if(valid && (tmpSeq != mSeq)) {
        mSeq = tmpSeq;
        mDiceValue = tmpValue;
        QString str = QString::asprintf(u8"第%d次投掷骰子,点数为:%d", mSeq, mDiceValue);
        ui->plainTextEdit->appendPlainText(str);
    }
}




推荐阅读