QT中多线程QWaitCondition例子解析

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
#include 
#include 

QMutex mutex;
QWaitCondition waitCon;
int number=6;
int count=0;
void MyThreadA::run()
{
    {
        Qt::HANDLE h = QThread::currentThreadId();
        qDebug()<<"AAAAAA begin running"<0)
//    {

//        mutex.unlock();
//        QThread::sleep(1);
//        mutex.lock();
//    }
    waitCon.wakeAll();
    //mutex.unlock();

    //a.wait();
    //b.wait();
    //c.wait();
    //a.run(); //单线程执行 整个程序是一个单线程
       //b.run(); //单线程执行 整个程序是一个单线程
       //a.terminate();
       //b.terminate();
       //a.wait();
       //b.wait();
}




MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

void MainWindow::on_pushButton_clicked()
{
    {
        a.start();
        b.start();
        c.start();//! 3个线程同时启动

        Qt::HANDLE h = QThread::currentThreadId();
        qDebug()<<"on_pushButton_clicked start  "<

QT中多线程QWaitCondition例子解析_第1张图片


下面是给一个线程传递不同参数的代码:

renderthread.h:

#ifndef RENDERTHREAD_H
#define RENDERTHREAD_H

#include 
#include 
#include 
#include 

//! [0]
class RenderThread : public QThread
{
    Q_OBJECT

public:
    RenderThread(QObject *parent = 0);
    ~RenderThread();

    void render(QString str1,QString str2);

signals:
    void renderedImage(const QString &image, QString scaleFactor);

protected:
    void run() Q_DECL_OVERRIDE;

private:
    QMutex mutex;
    QWaitCondition condition;
    QString str1;
    QString str2;

    bool restart;
    bool abort; //! 用于在析构的时候 退出RUN里面的循环

    enum { ColormapSize = 512 };
    uint colormap[ColormapSize];
};
//! [0]

#endif // RENDERTHREAD_H


renderthread.cpp

#include "renderthread.h"

#include 
#include 

//! [0]
RenderThread::RenderThread(QObject *parent)
    : QThread(parent)
{
    restart = false;
    abort = false;
}
RenderThread::~RenderThread()
{
    mutex.lock();
    abort = true;
    //condition.wakeOne();
    condition.wakeAll();
    mutex.unlock();
    wait();
}
//! [1]

//! [2] -- 给线程 RenderThread 传值 同时开始让线程开始执行  -- 此为在主线程中
void RenderThread::render(QString str1, QString str2)
{
    QMutexLocker locker(&mutex);

    //! 线程成员变量赋值 b
    this->str1 = str1;
    this->str2 = str2;
    //! 线程成员变量赋值 e


    if (!isRunning()) //! 若线程没有执行,则执行线程
    {
        Qt::HANDLE h = QThread::currentThreadId();
        qDebug()<<"start(LowPriority);   "<resultSize;
//        double scaleFactor = this->scaleFactor;
//        double centerX = this->centerX;
//        double centerY = this->centerY;
//        mutex.unlock();

        //! 下面进行数据处理

        while(1)
        {
            if (abort) //! 如果终止线程,直接return  
			{
				return;
			}
            if (restart) //! 如果重启,跳出循环
            {
                qDebug()<<"RenderThread::run()-- restart -  "<


调用方:
void MainWindow::on_pushButton_clicked()
{
    Qt::HANDLE h = QThread::currentThreadId();
    qDebug()<<"on_pushButton_clicked   "<
void RenderThread::stopThread()
{
    mutex.lock();
    abort = true;
    //condition.wakeOne();
    condition.wakeAll();
    mutex.unlock();
    wait();
    qDebug()<<"RenderThread::stopThread()()--";
}
void MainWindow::on_stop_clicked()
{
    thread.stopThread();
}
---可以不停的停止线程,然后开启线程


下面是效果图:

on_pushButton_2_clicked    0x3b08
condition.wakeAll();    0x3b08
RenderThread::run()-- restart -   "22222" "bbbbtttt" 0x36f0
RenderThread::run()-----   "22222" "bbbbtttt" 0x36f0

on_pushButton_clicked    0x3b08
condition.wakeAll();    0x3b08
RenderThread::run()-- restart -   "1111" "bbbb" 0x36f0
RenderThread::run()-----   "1111" "bbbb" 0x36f0

on_pushButton_2_clicked    0x3b08
condition.wakeAll();    0x3b08
RenderThread::run()-- restart -   "22222" "bbbbtttt" 0x36f0
RenderThread::run()-----   "22222" "bbbbtttt" 0x36f0





你可能感兴趣的:(QT学习)