网站开发 安全合同,中国工商注册网企业年报系统,网站设计服务费英文,wordpress get attachment.h代码示例
#ifndef THREAD_14_2_3_H
#define THREAD_14_2_3_H/*
功能描述#xff1a;使用三个线程 分别对公共资源进行操作
三个thread分别是一个生产者 两个消费者
写入#xff1a; 将自己的资源写入公共资源中去
thread#xff1a;str1 保存的来自公共资源
threadmain使用三个线程 分别对公共资源进行操作 三个thread分别是一个生产者 两个消费者 写入 将自己的资源写入公共资源中去 threadstr1 保存的来自公共资源 threadmainstr公共资源 被其他资源读时 可以 但是不能一读一写。 规定 thread1 是生产者 thread 2 3 是消费者 */#includeQWidget#includeQPushButton#includeQHBoxLayout#includeQVBoxLayout#includeQReadLocker#includeQWriteLocker#includeQLabel#includeQPlainTextEdit#includeQThread#includeQMutex#includeQReadWriteLock#includeQTimeclassthreads;/* 生产者功能 添加对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*parentnullptr);~threads();private:QPushButton*btnThread1Readnullptr;QPushButton*btnThread1Writenullptr;QLabel*thread1Labelnullptr;QPlainTextEdit*thread1Textnullptr;QLabel*thread2Labelnullptr;QPushButton*btnThread2Readnullptr;QPlainTextEdit*thread2Textnullptr;QLabel*thread3Labelnullptr;QPushButton*btnThread3Readnullptr;QPlainTextEdit*thread3Textnullptr;QHBoxLayout*tophlaynullptr;QHBoxLayout*centerhlaynullptr;QHBoxLayout*bottomhlaynullptr;QVBoxLayout*vlaynullptr;voidsetui();privateslots:voidreadthread1();voidwritethread1();voidreadthread2();voidreadthread3();voidprinttext(QString str1);private:voidconnectSignals();public:thread1*t1newthread1(this);thread2*t2newthread2(this);thread3*t3newthread3(this);enumStatue{stopped,running,paused};Statue statuepaused;private:QMutex mutex;public:QString buff1公共资源buff ;QReadWriteLock lock;};#endif// THREAD_14_2_3_H###代码.cpp#includethread_14_2_3.h#includeQString#includeQTimerthreads::threads(QWidget*parent):QWidget{parent}{setui();connectSignals();t1-start();t2-start();t3-start();}threads::~threads(){this-statuestopped;t1-quit();t1-wait();t2-quit();t2-wait();t3-quit();t3-wait();}voidthreads::setui(){QSize btnSize{60,30};btnThread1ReadnewQPushButton(this);btnThread1Read-setMinimumSize(btnSize);btnThread1Read-setText(读取);thread1LabelnewQLabel(this);thread1Label-setText(thread1);btnThread1WritenewQPushButton(this);btnThread1Write-setMinimumSize(btnSize);btnThread1Write-setText(写入buff1);thread1TextnewQPlainTextEdit(this);btnThread2ReadnewQPushButton(this);btnThread2Read-setMinimumSize(btnSize);btnThread2Read-setText(读取);thread2LabelnewQLabel(this);thread2Label-setText(thread2);thread2TextnewQPlainTextEdit(this);btnThread3ReadnewQPushButton(this);btnThread3Read-setMinimumSize(btnSize);btnThread3Read-setText(读取);thread3LabelnewQLabel(this);thread3Label-setText(thread3);thread3TextnewQPlainTextEdit(this);tophlaynewQHBoxLayout();centerhlaynewQHBoxLayout();bottomhlaynewQHBoxLayout();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);vlaynewQVBoxLayout(this);vlay-addLayout(tophlay);vlay-addLayout(centerhlay);vlay-addLayout(bottomhlay);this-setLayout(vlay);}//写线程 展示所有的线程的资源voidthreads::readthread1(){lock.tryLockForRead(1000);QString strallQString(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 str1thread2Text-toPlainText();t1-str1str1;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 str1QTime::currentTime().toString();parent-buff1str1;QString allQString(%1写入成功).arg(str1);emitwritetext(all);msleep(300);parent-lock.unlock();if(parent-statuethreads::stopped)break;}}voidthread1::write(){parent-buff1this-str1;}thread2::thread2(threads*parent):parent(parent){}thread2::~thread2(){}voidthread2::run(){while(1){msleep(500);QString str读取失败 芜湖 起飞;qDebug()报告长官消费者thread2号正在读取;if(parent-lock.tryLockForRead(50)){strparent-buff1;this-str1str;QString allQString(%1读取成功).arg(str);emitwritetext(all);msleep(300);parent-lock.unlock();}else{emitwritetext(str);msleep(150);}if(parent-statuethreads::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)){strparent-buff1;this-str1str;QString allQString(%1读取成功).arg(str);emitwritetext(all);msleep(300);parent-lock.unlock();}else{emitwritetext(str);msleep(150);}if(parent-statuethreads::stopped)break;}}