news 2025/12/17 15:10:02

程序:三线程并发 生产者 消费者---QT

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
程序:三线程并发 生产者 消费者---QT

.h代码示例

#ifndefTHREAD_14_2_3_H#defineTHREAD_14_2_3_H/* 功能描述:使用三个线程 分别对公共资源进行操作 三个thread分别是一个生产者 两个消费者 写入: 将自己的资源写入公共资源中去 thread:str1 保存的来自公共资源 threadmain:str:公共资源 被其他资源读时 可以 ,但是不能一读一写。 规定 thread1 是生产者 thread 2 3 是消费者 */#include<QWidget>#include<QPushButton>#include<QHBoxLayout>#include<QVBoxLayout>#include<QReadLocker>#include<QWriteLocker>#include<QLabel>#include<QPlainTextEdit>#include<QThread>#include<QMutex>#include<QReadWriteLock>#include<QTime>classthreads;/* 生产者功能 添加:对buff进行添加 展示buff 里面都有什么东西 */classthread1:publicQThread{Q_OBJECTpublic:thread1(threads*parent);~thread1();public:voidrun()override;public:QString str1="生产者thread:";threads*parent;voidwrite();signals:voidwritetext(QString str);};/* 消费者 从这里面读取资源*/classthread2:publicQThread{Q_OBJECTpublic:thread2(threads*parent);~thread2();public:voidrun()override;public:QString str1="读者线程2:";threads*parent;signals:voidwritetext(QString str);};classthread3:publicQThread{Q_OBJECTpublic:thread3(threads*parent);~thread3();public:voidrun()override;public:QString str1="读者线程3:";threads*parent;signals:voidwritetext(QString str);};classthreads:publicQWidget{Q_OBJECTpublic:threads(QWidget*parent=nullptr);~threads();private:QPushButton*btnThread1Read=nullptr;QPushButton*btnThread1Write=nullptr;QLabel*thread1Label=nullptr;QPlainTextEdit*thread1Text=nullptr;QLabel*thread2Label=nullptr;QPushButton*btnThread2Read=nullptr;QPlainTextEdit*thread2Text=nullptr;QLabel*thread3Label=nullptr;QPushButton*btnThread3Read=nullptr;QPlainTextEdit*thread3Text=nullptr;QHBoxLayout*tophlay=nullptr;QHBoxLayout*centerhlay=nullptr;QHBoxLayout*bottomhlay=nullptr;QVBoxLayout*vlay=nullptr;voidsetui();privateslots:voidreadthread1();voidwritethread1();voidreadthread2();voidreadthread3();voidprinttext(QString str1);private:voidconnectSignals();public:thread1*t1=newthread1(this);thread2*t2=newthread2(this);thread3*t3=newthread3(this);enumStatue{stopped,running,paused};Statue statue=paused;private:QMutex mutex;public:QString buff1="公共资源buff ";QReadWriteLock lock;};#endif// THREAD_14_2_3_H

###代码.cpp

#include"thread_14_2_3.h"#include<QString>#include<QTimer>threads::threads(QWidget*parent):QWidget{parent}{setui();connectSignals();t1->start();t2->start();t3->start();}threads::~threads(){this->statue=stopped;t1->quit();t1->wait();t2->quit();t2->wait();t3->quit();t3->wait();}voidthreads::setui(){QSize btnSize={60,30};btnThread1Read=newQPushButton(this);btnThread1Read->setMinimumSize(btnSize);btnThread1Read->setText("读取");thread1Label=newQLabel(this);thread1Label->setText("thread1");btnThread1Write=newQPushButton(this);btnThread1Write->setMinimumSize(btnSize);btnThread1Write->setText("写入buff1");thread1Text=newQPlainTextEdit(this);btnThread2Read=newQPushButton(this);btnThread2Read->setMinimumSize(btnSize);btnThread2Read->setText("读取");thread2Label=newQLabel(this);thread2Label->setText("thread2");thread2Text=newQPlainTextEdit(this);btnThread3Read=newQPushButton(this);btnThread3Read->setMinimumSize(btnSize);btnThread3Read->setText("读取");thread3Label=newQLabel(this);thread3Label->setText("thread3");thread3Text=newQPlainTextEdit(this);tophlay=newQHBoxLayout();centerhlay=newQHBoxLayout();bottomhlay=newQHBoxLayout();tophlay->addWidget(thread1Label);tophlay->addWidget(btnThread1Read);tophlay->addWidget(btnThread1Write);tophlay->addWidget(thread1Text);centerhlay->addWidget(thread2Label);centerhlay->addWidget(btnThread2Read);centerhlay->addWidget(thread2Text);bottomhlay->addWidget(thread3Label);bottomhlay->addWidget(btnThread3Read);bottomhlay->addWidget(thread3Text);vlay=newQVBoxLayout(this);vlay->addLayout(tophlay);vlay->addLayout(centerhlay);vlay->addLayout(bottomhlay);this->setLayout(vlay);}//写线程 展示所有的线程的资源voidthreads::readthread1(){lock.tryLockForRead(1000);QString strall=QString("buff1:%1\nthread1:%2\n读线程thread2:%3\n读线程thread3:%4").arg(buff1).arg(t1->str1).arg(t2->str1).arg(t3->str1);thread1Text->appendPlainText(strall);lock.unlock();}//写线程 把 编辑框里面的内容 写入t1内voidthreads::writethread1(){lock.lockForWrite();QString str1=thread2Text->toPlainText();t1->str1=str1;t1->write();thread3Text->appendPlainText(QString("正在写入---buff1%1").arg(str1));QThread::msleep(700);//模拟5s的时间,写入时间lock.unlock();}//thread2 读公共资源到自己的成员中voidthreads::readthread2(){// thread3Text->appendPlainText(QString("正在尝试读取-buff1上锁\n"));// if(lock.tryLockForRead())// {// t2.str1 = buff1;// thread3Text->appendPlainText(QString("thread2正在读取---buff1%1").arg(t2.str1));// lock.unlock();// return;// }// thread3Text->appendPlainText(QString("thread2读取失败---str=%1").arg(t2.str1));}voidthreads::readthread3(){// thread3Text->appendPlainText(QString("正在尝试读取-buff1上锁\n"));// if(lock.tryLockForRead())// {// t3.str1 = buff1;// thread3Text->appendPlainText(QString("thread3正在读取---buff1%1").arg(t3.str1));// lock.unlock();// }// thread3Text->appendPlainText(QString("thread3读取失败---str=%1").arg(t3.str1));}voidthreads::printtext(QString str1){thread3Text->appendPlainText(str1);}voidthreads::connectSignals(){connect(btnThread1Read,&QPushButton::clicked,this,&threads::readthread1);connect(btnThread2Read,&QPushButton::clicked,this,&threads::readthread2);connect(btnThread3Read,&QPushButton::clicked,this,&threads::readthread3);connect(btnThread1Write,&QPushButton::clicked,this,&threads::writethread1);connect(t1,&thread1::writetext,this,&threads::printtext);connect(t2,&thread2::writetext,this,&threads::printtext);connect(t3,&thread3::writetext,this,&threads::printtext);}thread1::thread1(threads*parent):parent(parent){}thread1::~thread1(){}voidthread1::run(){while(1){msleep(1000);qDebug()<<"报告长官,生产者thread1号正在生产";parent->lock.lockForWrite();QString str1=QTime::currentTime().toString();parent->buff1=str1;QString all=QString("%1写入成功").arg(str1);emitwritetext(all);msleep(300);parent->lock.unlock();if(parent->statue==threads::stopped)break;}}voidthread1::write(){parent->buff1=this->str1;}thread2::thread2(threads*parent):parent(parent){}thread2::~thread2(){}voidthread2::run(){while(1){msleep(500);QString str="读取失败 芜湖! 起飞!";qDebug()<<"报告长官,消费者thread2号正在读取";if(parent->lock.tryLockForRead(50)){str=parent->buff1;this->str1=str;QString all=QString("%1读取成功").arg(str);emitwritetext(all);msleep(300);parent->lock.unlock();}else{emitwritetext(str);msleep(150);}if(parent->statue==threads::stopped)break;}}thread3::thread3(threads*parent):parent(parent){}thread3::~thread3(){}voidthread3::run(){while(1){msleep(500);QString str="读取失败 芜湖! 起飞!";qDebug()<<"报告长官,消费者thread3号正在读取";if(parent->lock.tryLockForRead(50)){str=parent->buff1;this->str1=str;QString all=QString("%1读取成功").arg(str);emitwritetext(all);msleep(300);parent->lock.unlock();}else{emitwritetext(str);msleep(150);}if(parent->statue==threads::stopped)break;}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2025/12/17 15:10:00

DeeplxFile:突破文件翻译限制的终极解决方案

还在为文档翻译发愁吗&#xff1f;&#x1f629; 当其他翻译工具告诉你"文件太大"或"不支持Excel"时&#xff0c;DeeplxFile正在默默打破这些限制&#xff01;这款基于Deeplx和Playwright的开源工具&#xff0c;让免费、无限制的文件翻译成为现实。 【免费…

作者头像 李华
网站建设 2025/12/17 15:09:57

针对机械设备行业一体化项目制管理解决方案

对于机械设备工厂&#xff0c;管理中的挑战主要体现在内部流程的协调与效率、成本的控制&#xff0c;以及对定制化生产的管理上。机械设备行业专业的管理软件&#xff0c;正是为了针对性解决这些问题而设计的。机械设备工厂的管理痛点机械设备工厂的管理挑战复杂且具体&#xf…

作者头像 李华
网站建设 2025/12/17 15:09:32

别再问资质认证怎么查了!看这家公司如何用“大模型搜索”帮客户7天拿下高新认证

在数字化转型浪潮与政策红利双重叠加的今天&#xff0c;资质认证已成为企业提升核心竞争力、获取税收优惠、赢得市场先机的“硬通货”。无论是高新技术企业认定、专精特新申报&#xff0c;还是各类行业许可&#xff0c;其办理过程却常让企业主们头疼不已&#xff1a;办理流程不…

作者头像 李华
网站建设 2025/12/17 15:08:32

【量子编程数据同步新突破】:如何在Q#和Python间无缝传递变量?

第一章&#xff1a;Q#-Python 变量同步概述在量子计算与经典计算混合编程的场景中&#xff0c;Q# 与 Python 的协同工作变得愈发重要。变量同步是实现两者高效交互的核心机制之一&#xff0c;它允许 Q# 编写的量子操作与 Python 管理的经典数据之间进行无缝传递和状态共享。变量…

作者头像 李华
网站建设 2025/12/17 15:08:26

Java后端开发常见报错及解决方案:小白与大牛的问答故事

Java后端开发常见报错及解决方案&#xff1a;小白与大牛的问答故事 在学习Java后端开发过程中&#xff0c;遇到各种报错和Bug是常有的事。本文通过小白与资深Java大牛的对话方式&#xff0c;分享常见问题及解决方案&#xff0c;助你快速成长。第一轮问答 小白: 我在使用HashMap…

作者头像 李华
网站建设 2025/12/17 15:07:25

DeepSeek-V3实战指南:如何精准调优batch_size解决推理性能瓶颈

DeepSeek-V3实战指南&#xff1a;如何精准调优batch_size解决推理性能瓶颈 【免费下载链接】DeepSeek-V3 项目地址: https://gitcode.com/GitHub_Trending/de/DeepSeek-V3 当用户请求激增时&#xff0c;你是否发现AI模型响应变慢&#xff0c;GPU利用率却不高&#xff1…

作者头像 李华