一个Qt线程的例子,用于说明QWaitCondition的作用
描述可能比较麻烦,还是直接上代码吧!
main.cpp
- #include <QApplication>
- #include "mainpage.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainPage page;
- page.show();
- return a.exec();
- }
mainpage.h
- #ifndef MAINPAGE_H
- #define MAINPAGE_H
- #include <QWidget>
- #include "thread1.h"
- class MainPage : public QWidget
- {
- Q_OBJECT
- public:
- explicit MainPage(QWidget *parent = 0);
- ~MainPage();
- thread1 *th1;
- private slots:
- void doClick();
- };
- #endif // MAINPAGE_H
thread1.h
- #ifndef THREAD1_H
- #define THREAD1_H
- #include <QThread>
- #include <QWaitCondition>
- #include "thread2.h"
- #include <QMutex>
- class thread1 : public QThread
- {
- Q_OBJECT
- public:
- explicit thread1(QObject *parent = 0);
- void run();
- void wakeFunc();
- private:
- QWaitCondition cond;
- QMutex mutex;
- thread2 th2;
- };
- #endif // THREAD1_H
thread2.h
- #ifndef THREAD2_H
- #define THREAD2_H
- #include <QThread>
- class thread2 : public QThread
- {
- Q_OBJECT
- public:
- explicit thread2(QObject *parent = 0);
- void run();
- };
- #endif // THREAD2_H
mainpage.cpp
- #include "mainpage.h"
- #include <QDebug>
- #include <QPushButton>
- MainPage::MainPage(QWidget *parent) :
- QWidget(parent)
- {
- th1=new thread1;
- this->resize(100, 40);
- QPushButton *pb=new QPushButton("按钮", this);
- pb->resize(50, 20);
- pb->move(10, 10);
- connect(pb, SIGNAL(clicked()), this, SLOT(doClick()));
- }
- MainPage::~MainPage()
- {
- }
- void MainPage::doClick()
- {
- qDebug()<<"button clicked!";
- th1->wakeFunc();
- }
thread1.cpp
- #include "thread1.h"
- #include <QDebug>
- thread1::thread1(QObject *parent) :
- QThread(parent)
- {
- }
- void thread1::wakeFunc()
- {
- if(!isRunning()){
- start();
- }else{
- cond.wakeOne();
- }
- }
- void thread1::run()
- {
- static int x=0;
- qDebug()<<"进入thread1的run函数";
- while(true){
- qDebug()<<"-------> 1 <---------";
- mutex.lock();
- if(++x>3) th2.start();
- qDebug() << "进入thread1的while循环,x值为"<<x;
- cond.wait(&mutex); //当处于wait状态时mutex会被暂时释放,并阻塞在这个地方;当线程被cond.wakeOne()等唤醒时,mutex又会被重新锁定,并继续运行
- mutex.unlock();
- qDebug()<<"-------> 2 <---------";
- }
- }
thread2.cpp
- #include "thread2.h"
- #include <QDebug>
- thread2::thread2(QObject *parent) :
- QThread(parent)
- {
- }
- void thread2::run()
- {
- qDebug()<<"线程2已激活";
- }
运行效果:
(1)按钮点击一次时的调试输出
(3)按钮点击多次时的调试输出
结论:借助网友的说法,在debug里面可以看到线程1的run函数只进入了一次,虽然我们调用了它很多次。也就是说使用waitCondition可以让线程进入休眠而不用退出。
一个Qt线程的例子,用于说明QWaitCondition的作用的更多相关文章
- qt 中创建一个工作线程(例子)
当一个事件需要很长的处理时间,就创建一个工作线程,防止主界面卡死. 1.新建一个QT的gui项目,里面包含main.cpp,mainwindow.h,mainwindow.cpp,mainwindow ...
- 一个Java线程小例子(仿火车票售卖)
public class MyThread extends Thread{ private static int ticket=100; public void run(){ for(int i=0; ...
- 一个LinkedBlockingQueue线程安全的例子
一个LinkedBlockingQueue线程安全的例子 package llj.mf.ace; import java.util.ArrayList; import java.util.HashSe ...
- QT线程使用收集示例
关于多线程问题: Qt和Boost做跨平台的线程封装,OpenMP主要做并行计算,让不精通多线程的人也能高效地利用CPU的计算能力.个人倾向于用boost.thread, boost.mpi. 一 ...
- qt线程(转)----这篇很专业!
本文档是自己所整理的一份文档,部分是原创,还转贴了网上的一此资料(已经标明了),(难点是多线程的编写),是有源代码的,大家可以作为参考,用到的知识是视频采集,压缩解压(xvid),实时传输(jrtp) ...
- QT核心编程之Qt线程 (c)
QT核心编程之Qt线程是本节要介绍的内容,QT核心编程我们要分几个部分来介绍,想参考更多内容,请看末尾的编辑推荐进行详细阅读,先来看本篇内容. Qt对线程提供了支持,它引入了一些基本与平台无关的线程类 ...
- Qt 线程基础(Thread Basics的翻译,线程的五种使用情况)
Qt 线程基础(QThread.QtConcurrent等) 转载自:http://blog.csdn.net/dbzhang800/article/details/6554104 昨晚看Qt的Man ...
- Qt 线程基础(QThread、QtConcurrent等)
[-] 使用线程 何时使用其他技术替代线程 应该使用 Qt 线程的哪种技术 Qt线程基础 QObject与线程 使用互斥量保护数据的完整 使用事件循环防止数据破坏 处理异步执行 昨晚看Qt的Manua ...
- Qt 线程基础
(转自:http://my.oschina.net/laopiao/blog/88158) 何谓线程? 线程与并行处理任务息息相关,就像进程一样.那么,线程与进程有什么区别呢?当你在电子表格上进行数据 ...
随机推荐
- day 02 while 循环 格式化输出 运算符 and or not - 编码的初识
while 循环 while 条件: 循环体 循环如何终止? 改变条件. flag = Truewhile flag: print('狼的诱惑') print('我们不一样') ...
- Win10下编译OpenJDK8
导航目录 Win10下编译OpenJDK8 相关参考文章 编译环境 编译前准备 1.安装 Visual Studio 2010 Professional 2. 准备OpenJDK8 3. 编译JDK环 ...
- Flask开发系列之数据库操作
Flask开发系列之数据库操作 Python数据库框架 我们可以在Flask中使用MySQL.Postgres.SQLite.Redis.MongoDB 或者 CouchDB. 还有一些数据库抽象层代 ...
- 微软撤出 Windows断供华为!
华为被美国列入“实体名单”后,从硬件到软件再到技术标准,华为对外联系纷纷被掐断,其中软件系统方面,Google安卓系统已经停止与华为合作,Mate 20 Pro也被从安卓Q 10.0的尝鲜名单中移除. ...
- PAT Basic 1076 Wifi密码 (15 分)
下面是微博上流传的一张照片:“各位亲爱的同学们,鉴于大家有时需要使用 wifi,又怕耽误亲们的学习,现将 wifi 密码设置为下列数学题答案:A-1:B-2:C-3:D-4:请同学们自己作答,每两日一 ...
- react-native启动时红屏报错:Unable to load script.Make sure you're either running a metro server or that ....
一.报错信息内容 我是在Android Studio中运行启动react-native项目时报的这个错误 1.报错提示:Unable to load script.Make sure you're e ...
- python 控制流(二)
常用控制流 条件语句 循环语句 一.条件语句 if 条件表达式: #条件表达式--->比较运算符--->布尔值 满足条件表达式执行的代码块 #当布尔值为 True时执行此句 elif 条件 ...
- 安装 mysql odbc连接器
下载地址: https://dev.mysql.com/downloads/connector/odbc/ 可以选择旧版本的下载 一. 配置数据源 1. 安装后如果找不到软件可以按 win键 后 输 ...
- 集合操作符 Union / Union All / Intersect / Minus
集合操作符 Union / UnionAll / Intersect / Minus -- 生成测试数据 create table dept_01 as select * from dept wher ...
- vue导航菜单动态展示
地址:https://blog.csdn.net/qq_31126175/article/details/81875468