#include "mainw.h"
#include "clockthread.h"
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent)
{
clock_thread1 = new ClockThread(1,this);
connect(clock_thread1, SIGNAL(sigReady(int)), this, SLOT(slot_updateClock(int)));
connect(this,SIGNAL(startWork()),clock_thread1, SLOT(slotWork()));
clock_thread2 = new ClockThread(2,this);
connect(clock_thread2, SIGNAL(sigReady(int)), this, SLOT(slot_updateClock(int)));
connect(this,SIGNAL(startWork()),clock_thread2, SLOT(slotWork()));
ready[0]=0;
ready[1]=0;
clock_thread1->set_thread_runing(true);
clock_thread2->set_thread_runing(true);
// clock_thread1->start(); //thread 不运行,signal slot 照样运行
// clock_thread2->start();
emit startWork();
}
MainWindow::~MainWindow()
{
//如果析构函数为空,当然会有
//QThread: Destroyed while thread is still running
//已放弃
//如果不停止线程而wait, 会卡死.
clock_thread1->set_thread_runing(false);
clock_thread2->set_thread_runing(false);
//停止了线程,必须等待线程结束, 否则也会有
//QThread: Destroyed while thread is still running
//已放弃
clock_thread1->wait();
clock_thread2->wait();
//这句delete 倒是可有可无的,不会内存泄露,此处不加
//MainWindow 的析构中会调用delete, 因为clock_thread 是MainWindow 的子对象
delete clock_thread1;
delete clock_thread2;
}
extern long gettime_us();
void MainWindow::slot_updateClock(int id)
{
long time=gettime_us();
printf("main receive work ready signal.id:%d,time:%ld\n",id,time/1000);
ready[id-1]=1;
if(ready[0]==1 && ready[1]==1)
{
clock_thread1->set_thread_runing(false);
clock_thread2->set_thread_runing(false);
clock_thread1->wait();
clock_thread2->wait();
ready[0]=0; //初始化
ready[1]=0;
clock_thread1->set_thread_runing(true);
clock_thread2->set_thread_runing(true);
clock_thread1->start();
clock_thread2->start();
emit startWork(); //再一次启动工作
}
}
Qt5 信号与槽研究,线程研究
需积分: 32 82 浏览量
2022-03-12
10:58:10
上传
评论 1
收藏 494KB ZIP 举报
hjjdebug
- 粉丝: 565
- 资源: 45
最新资源
- YOLOV4-TINY权重文件
- 以下是一个使用贪心算法解决多机调度问题的基本步骤0.txt
- 基于大数据的房产估价是近年来随着技术的发展而兴起的一种新型估价方法.txt
- 企业供应链管理系统v3.rar
- 富芮坤FR8016HA蓝牙开发板使用手册+硬件PCB图+封装库+DEMO演示软件源代码.zip
- 基于YOLOv7的芯片表面缺陷检测系统
- 京东物流 数字化供应链综合研究报告2018.rar
- 基于YOLOv7的植物虫害识别&防治系统
- 2000.1-2023.8中国经济政策不确定性指数月度数据.xlsx
- Screenshot_2024-04-21-20-42-15-443_com.tencent.mm.jpg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0