zoukankan      html  css  js  c++  java
  • Qt线程初步

    默默地EEer,原文地址:

    http://www.cnblogs.com/hebaichuanyeah/p/5618781.html

    在调用linux环境下线程API写Cpp时,发现异常蛋疼,要么线程函数不能是类的成员函数,要么必须是类的static成员函数(意味着调用的成员变量也必须是static……其实还是不属于类)。

    Qt提供一套线程机制,只要继承QThread基类并重新实现void run()函数即可。

    新建thread.h继承Qthread,并在thread.cpp中实现它。

    thread.h

    #include "QThread"
    
    class Thread : public QThread
    {
        Q_OBJECT
    public:
        Thread();
     //   void setMessage(const Qstring &message);
        void setRun();
        void stop();
        bool getState();
    protected:
        void run();
    private:
       // QString messageStr;
        volatile bool enable; 
    signals:
        void OutMessage(QString message);
    };
    

     thread.cpp,在run函数中循环1秒触发一个信号输出信息。

    #include "thread.h"
    
    Thread::Thread()
    {
        enable = true;
    }
    
    void Thread::run()
    {
        while(true)
        {
            if(enable)
            {
                this->OutMessage(tr("I'm run : thread 1"));
                msleep(1000);
            }
        }
    }
    void Thread::stop()
    {
        enable = false;
    }
    void Thread::setRun()
    {
        enable = true;
    }
    bool  Thread::getState()
    {
        return enable;
    }
    

     mainwindow.h,定义一些Qt控件

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    
    #include <QMainWindow>
    #include <QLabel>
    #include <QGridLayout>
    #include <QPushButton>
    #include <QTextEdit>
    #include <QDoubleValidator>
    #include <QComboBox>
    #include <QTranslator>
    
    #include "thread.h"
    
    #define PI  3.14592653589
    
    class MainWindow  : public QMainWindow
    {
        Q_OBJECT
    public:
        MainWindow();
        ~MainWindow();
    private:
        QWidget* mainWindow;
        QGridLayout * mainLayout;
        QTextEdit * messageEdit;
        QPushButton * clearButton;
        QPushButton * runStopButton;
        Thread * backThread;
    private slots:
        void updateMessage(QString message);
        void clearMessage();
        void runStopThread();
    
    };
    
    #endif
    

     mainwindow.cpp

    #include "mainwindow.h"
    #include <QPainter>
    
    
    MainWindow :: MainWindow()
    {
        mainWindow = new QWidget;
        this->setCentralWidget(mainWindow);
    
        backThread = new Thread();
        backThread->start();
    
    
        clearButton = new QPushButton(tr("clear"));
        clearButton->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
        runStopButton = new QPushButton(tr("stop"));
        messageEdit = new QTextEdit();
        messageEdit->setReadOnly(true);
        mainLayout = new QGridLayout;
    
        mainLayout->addWidget(messageEdit,0,0,5,5);
        mainLayout->addWidget(clearButton,1,5,1,1);
        mainLayout->addWidget(runStopButton,0,5,1,1);
    
        mainWindow->setLayout(mainLayout);
    
        QObject::connect(clearButton,SIGNAL(clicked()),this,SLOT(clearMessage()));
        QObject::connect(runStopButton,SIGNAL(clicked()),this,SLOT(runStopThread()));
    
        QObject::connect(backThread,SIGNAL(OutMessage(QString)),this,SLOT(updateMessage(QString)));
    
        this->setWindowTitle(tr("thread test~"));
        this->resize(QSize(300,100));
     //   this->setFixedSize(200,100);
    }
    
    MainWindow :: ~MainWindow()
    {
        mainWindow->deleteLater();
        mainLayout->deleteLater();
        messageEdit->deleteLater();
        clearButton->deleteLater();
        runStopButton->deleteLater();
    }
    
    void MainWindow::updateMessage(QString message)
    {
        messageEdit->append(message);
    }
    void MainWindow::clearMessage()
    {
        messageEdit->clear();
    }
    void MainWindow::runStopThread()
    {
        if(backThread->getState())
        {
            backThread->stop();
            runStopButton->setText(tr("run"));
        }
        else
        {
            backThread->setRun();
            runStopButton->setText(tr("stop"));
    
        }     
    }
    
    

     运行:

    后台线程不断打印信息,通过run/stop按钮停止/启动。

    同时,Qt提供一套线程同步机制

    QMutex(互斥锁),QReadWriteLock(读写锁),QSemaphore(信号量),QWaitCondition(阻塞等待一个条件)。

    使用都很简单,其中QWaitCondition,调用bool wait(QMutex *mutex, unsigned long time = ULONG_MAX)成员函数阻塞。 void wakeOne()和void wakeAll()唤醒一处和所有阻塞。(类似于C#里面的ManualResetEvent 类,.WaitOne()   .Set())

  • 相关阅读:
    正则表达式贪婪与非贪婪模式
    GPGPU OpenCL 获取设备信息
    GPGPU OpenCL编程步骤与简单实例
    去掉linux 系统vi中出现^M字符的方法
    OpenMP 线程互斥锁
    CUDA使用Event进行程序计时
    Python日志库logging总结-可能是目前为止将logging库总结的最好的一篇文章
    Python-文件读写及修改
    有关Python的import...和from...import...的区别
    Python中调用其他程序的方式
  • 原文地址:https://www.cnblogs.com/hebaichuanyeah/p/5618781.html
Copyright © 2011-2022 走看看