.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;}}