zoukankan      html  css  js  c++  java
  • 一个Qt线程的例子,用于说明QWaitCondition的作用

     

     

    描述可能比较麻烦,还是直接上代码吧!

    main.cpp

    [cpp] view plain copy
     
    1. #include <QApplication>  
    2. #include "mainpage.h"  
    3.   
    4. int main(int argc, char *argv[])  
    5. {  
    6.     QApplication a(argc, argv);  
    7.   
    8.     MainPage page;  
    9.     page.show();  
    10.   
    11.     return a.exec();  
    12. }  

    mainpage.h
    [cpp] view plain copy
     
    1. #ifndef MAINPAGE_H  
    2. #define MAINPAGE_H  
    3.   
    4. #include <QWidget>  
    5.   
    6. #include "thread1.h"  
    7.   
    8.   
    9. class MainPage : public QWidget  
    10. {  
    11.     Q_OBJECT  
    12.   
    13. public:  
    14.     explicit MainPage(QWidget *parent = 0);  
    15.     ~MainPage();  
    16.   
    17.     thread1 *th1;  
    18.   
    19. private slots:  
    20.     void doClick();  
    21.   
    22. };  
    23.   
    24. #endif // MAINPAGE_H  

    thread1.h
    [cpp] view plain copy
     
    1. #ifndef THREAD1_H  
    2. #define THREAD1_H  
    3.   
    4. #include <QThread>  
    5. #include <QWaitCondition>  
    6. #include "thread2.h"  
    7. #include <QMutex>  
    8.   
    9. class thread1 : public QThread  
    10. {  
    11.     Q_OBJECT  
    12. public:  
    13.     explicit thread1(QObject *parent = 0);  
    14.   
    15.     void run();  
    16.     void wakeFunc();  
    17.   
    18. private:  
    19.     QWaitCondition cond;  
    20.     QMutex mutex;  
    21.     thread2 th2;  
    22. };  
    23.   
    24. #endif // THREAD1_H  

    thread2.h
    [cpp] view plain copy
     
    1. #ifndef THREAD2_H  
    2. #define THREAD2_H  
    3.   
    4. #include <QThread>  
    5.   
    6. class thread2 : public QThread  
    7. {  
    8.     Q_OBJECT  
    9. public:  
    10.     explicit thread2(QObject *parent = 0);  
    11.   
    12.     void run();  
    13. };  
    14.   
    15. #endif // THREAD2_H  

    mainpage.cpp
    [cpp] view plain copy
     
    1. #include "mainpage.h"  
    2. #include <QDebug>  
    3. #include <QPushButton>  
    4.   
    5. MainPage::MainPage(QWidget *parent) :  
    6.     QWidget(parent)  
    7. {  
    8.     th1=new thread1;  
    9.   
    10.     this->resize(100, 40);  
    11.   
    12.     QPushButton *pb=new QPushButton("按钮", this);  
    13.     pb->resize(50, 20);  
    14.     pb->move(10, 10);  
    15.     connect(pb, SIGNAL(clicked()), this, SLOT(doClick()));  
    16. }  
    17.   
    18. MainPage::~MainPage()  
    19. {  
    20. }  
    21.   
    22. void MainPage::doClick()  
    23. {  
    24.     qDebug()<<"button clicked!";  
    25.     th1->wakeFunc();  
    26. }  

    thread1.cpp
    [cpp] view plain copy
     
    1. #include "thread1.h"  
    2. #include <QDebug>  
    3.   
    4. thread1::thread1(QObject *parent) :  
    5.     QThread(parent)  
    6. {  
    7. }  
    8.   
    9. void thread1::wakeFunc()  
    10. {  
    11.     if(!isRunning()){  
    12.         start();  
    13.     }else{  
    14.         cond.wakeOne();  
    15.     }  
    16. }  
    17.   
    18. void thread1::run()  
    19. {  
    20.     static int x=0;  
    21.     qDebug()<<"进入thread1的run函数";  
    22.     while(true){  
    23.         qDebug()<<"-------> 1 <---------";  
    24.         mutex.lock();  
    25.         if(++x>3) th2.start();  
    26.         qDebug() << "进入thread1的while循环,x值为"<<x;  
    27.         cond.wait(&mutex);   //当处于wait状态时mutex会被暂时释放,并阻塞在这个地方;当线程被cond.wakeOne()等唤醒时,mutex又会被重新锁定,并继续运行  
    28.         mutex.unlock();  
    29.         qDebug()<<"-------> 2 <---------";  
    30.     }  
    31. }  

    thread2.cpp
    [cpp] view plain copy
     
    1. #include "thread2.h"  
    2. #include <QDebug>  
    3.   
    4. thread2::thread2(QObject *parent) :  
    5.     QThread(parent)  
    6. {  
    7. }  
    8.   
    9. void thread2::run()  
    10. {  
    11.     qDebug()<<"线程2已激活";  
    12. }  

    运行效果:

    (1)按钮点击一次时的调试输出

    (3)按钮点击多次时的调试输出

    结论:借助网友的说法,在debug里面可以看到线程1的run函数只进入了一次,虽然我们调用了它很多次。也就是说使用waitCondition可以让线程进入休眠而不用退出。

  • 相关阅读:
    ios特性访问器方法(setter和getter)
    JavaScript文本收缩展开 showdetail
    MSClass (Class Of Marquee Scroll通用不间断滚动JS封装类) Ver 1.65
    拖动层 拖动div 封装js 貌似不兼容FF,郁闷
    cookie中存储json
    页面get post等查看
    瑞星杀毒软件2010年1月11日8:29:28 【免费】
    div两侧的boder断开 消失 奇怪
    用来武装Firebug的十四款Firefox插件
    在内容页中调用母版页控件、响应控件消息(用户控件、服务器控件有所不同)
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/7787829.html
Copyright © 2011-2022 走看看