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

main.cpp

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

mainpage.h

  1. #ifndef MAINPAGE_H
  2. #define MAINPAGE_H
  3. #include <QWidget>
  4. #include "thread1.h"
  5. class MainPage : public QWidget
  6. {
  7. Q_OBJECT
  8. public:
  9. explicit MainPage(QWidget *parent = 0);
  10. ~MainPage();
  11. thread1 *th1;
  12. private slots:
  13. void doClick();
  14. };
  15. #endif // MAINPAGE_H

thread1.h

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

thread2.h

  1. #ifndef THREAD2_H
  2. #define THREAD2_H
  3. #include <QThread>
  4. class thread2 : public QThread
  5. {
  6. Q_OBJECT
  7. public:
  8. explicit thread2(QObject *parent = 0);
  9. void run();
  10. };
  11. #endif // THREAD2_H

mainpage.cpp

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

thread1.cpp

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

thread2.cpp

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

运行效果:

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

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

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

一个Qt线程的例子,用于说明QWaitCondition的作用的更多相关文章

  1. qt 中创建一个工作线程(例子)

    当一个事件需要很长的处理时间,就创建一个工作线程,防止主界面卡死. 1.新建一个QT的gui项目,里面包含main.cpp,mainwindow.h,mainwindow.cpp,mainwindow ...

  2. 一个Java线程小例子(仿火车票售卖)

    public class MyThread extends Thread{ private static int ticket=100; public void run(){ for(int i=0; ...

  3. 一个LinkedBlockingQueue线程安全的例子

    一个LinkedBlockingQueue线程安全的例子 package llj.mf.ace; import java.util.ArrayList; import java.util.HashSe ...

  4. QT线程使用收集示例

    关于多线程问题: Qt和Boost做跨平台的线程封装,OpenMP主要做并行计算,让不精通多线程的人也能高效地利用CPU的计算能力.个人倾向于用boost.thread, boost.mpi.   一 ...

  5. qt线程(转)----这篇很专业!

    本文档是自己所整理的一份文档,部分是原创,还转贴了网上的一此资料(已经标明了),(难点是多线程的编写),是有源代码的,大家可以作为参考,用到的知识是视频采集,压缩解压(xvid),实时传输(jrtp) ...

  6. QT核心编程之Qt线程 (c)

    QT核心编程之Qt线程是本节要介绍的内容,QT核心编程我们要分几个部分来介绍,想参考更多内容,请看末尾的编辑推荐进行详细阅读,先来看本篇内容. Qt对线程提供了支持,它引入了一些基本与平台无关的线程类 ...

  7. Qt 线程基础(Thread Basics的翻译,线程的五种使用情况)

    Qt 线程基础(QThread.QtConcurrent等) 转载自:http://blog.csdn.net/dbzhang800/article/details/6554104 昨晚看Qt的Man ...

  8. Qt 线程基础(QThread、QtConcurrent等)

    [-] 使用线程 何时使用其他技术替代线程 应该使用 Qt 线程的哪种技术 Qt线程基础 QObject与线程 使用互斥量保护数据的完整 使用事件循环防止数据破坏 处理异步执行 昨晚看Qt的Manua ...

  9. Qt 线程基础

    (转自:http://my.oschina.net/laopiao/blog/88158) 何谓线程? 线程与并行处理任务息息相关,就像进程一样.那么,线程与进程有什么区别呢?当你在电子表格上进行数据 ...

随机推荐

  1. C#下载图片,用户选择保存路径

    Html代码 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></ti ...

  2. node.js 设置静态文件托管

    1.在app.js文件中设置静态文件托管 /*应用程序入口文件*/ /*加载express模块*/ var express = require('express'); /*加载模板处理模块*/ var ...

  3. java 如何读取 properties 配置文件

  4. java.lang.ClassNotFoundException: org.apache.jsp.login_jsp

    <span style="font-family: Simsun; background-color: rgb(255, 255, 255);">想必大家在用Eclip ...

  5. Laravel 向公共模板赋值

    开发过程中许多时候都会向公共模板赋值,比如顶部导航栏,页面底部等等,不可能在每个控制器中都赋值一遍. Laravel 中解决办法如下:修改 App\Providers\AppServiceProvid ...

  6. cmd命令删除文件夹或文件

    删除空文件夹rd /s/q "D:\Program Files\afish" 删除里面的文件del /a/f/q "D:\Program Files\afish\123. ...

  7. 【转】原生js实现移动端h5长按事件

    $("#target").on({ touchstart: function(e) { // 长按事件触发 timeOutEvent = setTimeout(function() ...

  8. 集合综合练习<二>

    package com.JiHeTotal; import java.util.Map; public class Student { int id; String name; Map<Stri ...

  9. 任意修改网页内容JS代码

    浏览器输入框执行,chrome需要粘贴后,需要在前面手打javascript: 因为粘贴的会自动过滤 javascript:document.body.contentEditable='true'; ...

  10. 【NOIP2012模拟10.25】剪草

    题目 有N棵小草,编号0至N-1.奶牛Bessie不喜欢小草,所以Bessie要用剪刀剪草,目标是使得这N棵小草的高度总和不超过H.在第0时刻,第i棵小草的高度是h[i],接下来的每个整数时刻,会依次 ...