Qt入门实践Qt之哲学家问题(linux 多线程)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt入门实践Qt之哲学家问题(linux 多线程)相关的知识,希望对你有一定的参考价值。
转载请注明出处:http://blog.csdn.net/feng1790291543
linux多线程实现哲学家问题,依据哲学家吃饭、拿筷子、放下筷子......
首先,主线程使用Qt下的GUI的简单一个button控制即可了
maindesttop.cpp
#include "maindesttop.h" #include "ui_maindesttop.h" QMutex mutex01; QMutex mutex02; QMutex mutex03; QMutex mutex04; QMutex mutex05; int n1=1; int n2=1; int n3=1; int n4=1; int n5=1; QWaitCondition waitcondition001; QWaitCondition waitcondition002; QWaitCondition waitcondition003; QWaitCondition waitcondition004; QWaitCondition waitcondition005; MainDestTop::MainDestTop(QWidget *parent) : QWidget(parent), ui(new Ui::MainDestTop) { ui->setupUi(this); } MainDestTop::~MainDestTop() { delete ui; } void MainDestTop::on_pushButtonStart_clicked() { pro001=new Proferssor001; pro002=new Professor002; pro003=new Professor003; pro004=new Professor004; pro005=new Professor005; pro001->start(); pro002->start(); pro003->start(); pro004->start(); pro005->start(); return ; } void MainDestTop::StopThread() { pro001->quit(); pro001->wait(); pro001->deleteLater(); delete pro001; pro001=NULL; pro002->quit(); pro002->wait(); pro002->deleteLater(); delete pro002; pro002=NULL; pro003->quit(); pro003->wait(); pro003->deleteLater(); delete pro003; pro003=NULL; pro004->quit(); pro004->wait(); pro004->deleteLater(); delete pro004; pro004=NULL; pro005- >quit(); pro005->wait(); pro005->deleteLater(); delete pro005; pro005=NULL; }
maindesttop.h
#ifndef MAINDESTTOP_H #define MAINDESTTOP_H #include <QtGui/QWidget> #include <QtCore> #include <QMutex> #include "proferssor001.h" #include "professor002.h" #include "professor003.h" #include "professor004.h" #include "professor005.h" namespace Ui { class MainDestTop; } class MainDestTop : public QWidget { Q_OBJECT public: MainDestTop(QWidget *parent = 0); ~MainDestTop(); void StopThread(); private: Ui::MainDestTop *ui; Proferssor001 *pro001; Professor002 *pro002; Professor003 *pro003; Professor004 *pro004; Professor005 *pro005; private slots: void on_pushButtonStart_clicked(); }; #endif // MAINDESTTOP_H
其次。子线程源代码例如以下:
#ifndef PROFERSSOR001_H #define PROFERSSOR001_H #include <QThread> #include <QtCore> class Proferssor001 : public QThread { public: Proferssor001(); void run(); }; #endif // PROFERSSOR001_H
第一个线程源代码:
#include "proferssor001.h" /**************** proferssor001---->A proferssor002---->B proferssor003--->C proferssor004---->D proferssor005---->E 资源夺取: A--->1/5 B--->2/1 C--->3/2 D--->4/3 E--->5/4 ******************/ extern QMutex mutex01; extern QMutex mutex02; extern QMutex mutex03; extern QMutex mutex04; extern QMutex mutex05; extern int n1; extern int n2; extern int n3; extern int n4; extern int n5; extern QWaitCondition waitcondition001; extern QWaitCondition waitcondition002; extern QWaitCondition waitcondition003; extern QWaitCondition waitcondition004; extern QWaitCondition waitcondition005; Proferssor001::Proferssor001() { } void Proferssor001::run() { while(1) { mutex01.lock(); mutex05.lock(); while((n1+n5)<2) { mutex05.unlock(); qDebug()<<"proferssor001哲学家没左边筷子~"; mutex01.unlock(); qDebug()<<"proferssor001哲学家没右边筷子~"; qDebug()<<"proferssor001$$$$$$$$$$哲学家開始歇息...."; waitcondition001.wait(&mutex01); waitcondition005.wait(&mutex05); continue ; } qDebug()<<"proferssor001$$$$$$$$$$哲学家開始吃饭~"; n1--; n5--; mutex05.unlock(); n5=(n5+1); qDebug()<<"proferssor001$$$$$$$$$$哲学家放下左边筷子~"; msleep(2); mutex01.unlock(); n1=(n1+1); qDebug()<<"proferssor001$$$$$$$$$$哲学家放下右边筷子~"; msleep(2); } return ; }
第二线程头文件:
#ifndef PROFESSOR002_H #define PROFESSOR002_H #include <QThread> #include <QtCore> class Professor002 : public QThread { public: Professor002(); void run(); }; #endif // PROFESSOR002_H
源程序:
#include "professor002.h" /**************** proferssor001---->A proferssor002---->B proferssor003--->C proferssor004---->D proferssor005---->E 资源夺取: A--->1/5 B--->2/1 C--->3/2 D--->4/3 E--->5/4 ******************/ extern QMutex mutex01; extern QMutex mutex02; extern QMutex mutex03; extern QMutex mutex04; extern QMutex mutex05; extern int n1; extern int n2; extern int n3; extern int n4; extern int n5; extern QWaitCondition waitcondition001; extern QWaitCondition waitcondition002; extern QWaitCondition waitcondition003; extern QWaitCondition waitcondition004; extern QWaitCondition waitcondition005; Professor002::Professor002() { } void Professor002::run() { while(1) { mutex02.lock(); mutex01.lock(); while((n1+n2)<2) { mutex01.unlock(); qDebug()<<"proferssor002哲学家没左边筷子~"; mutex02.unlock(); qDebug()<<"proferssor002哲学家没右边筷子~"; qDebug()<<"proferssor002&&&&&&&&&哲学家開始歇息...."; waitcondition002.wait(&mutex02); waitcondition001.wait(&mutex01); continue ; } qDebug()<<"proferssor002&&&&&&&&&哲学家開始吃饭~"; n1--; n2--; mutex01.unlock(); n1=(n1+1); qDebug()<<"proferssor002&&&&&&&&&哲学家放下左边筷子~"; msleep(2); mutex02.unlock(); n2=(n2+1); qDebug()<<"proferssor002&&&&&&&&&哲学家放下右边筷子~"; msleep(2); } return ; }
线程三,头文件:
#ifndef PROFESSOR003_H #define PROFESSOR003_H #include <QThread> #include <QtCore> class Professor003 : public QThread { public: Professor003(); void run(); }; #endif // PROFESSOR003_H
源文件:
#include "professor003.h" /**************** proferssor001---->A proferssor002---->B proferssor003--->C proferssor004---->D proferssor005---->E 资源夺取: A--->1/5 B--->2/1 C--->3/2 D--->4/3 E--->5/4 ******************/ extern QMutex mutex01; extern QMutex mutex02; extern QMutex mutex03; extern QMutex mutex04; extern QMutex mutex05; extern int n1; extern int n2; extern int n3; extern int n4; extern int n5; extern QWaitCondition waitcondition001; extern QWaitCondition waitcondition002; extern QWaitCondition waitcondition003; extern QWaitCondition waitcondition004; extern QWaitCondition waitcondition005; Professor003::Professor003() { } void Professor003::run() { while(1) { mutex03.lock(); mutex02.lock(); while((n2+n3)<2) { mutex02.unlock(); qDebug()<<"proferssor003哲学家没左边筷子~"; mutex03.unlock(); qDebug()<<"proferssor003哲学家没右边筷子~"; qDebug()<<"proferssor003******哲学家開始歇息...."; waitcondition003.wait(&mutex03); waitcondition002.wait(&mutex02); continue ; } qDebug()<<"proferssor003******哲学家開始吃饭~"; n2--; n3--; mutex02.unlock(); n2=(n2+1); qDebug()<<"proferssor003******哲学家放下左边筷子~"; msleep(2); mutex03.unlock(); n3=(n3+1); qDebug()<<"proferssor003******哲学家放下右边筷子~"; msleep(2); } return ; }
线程4头文件:
#ifndef PROFESSOR004_H #define PROFESSOR004_H #include <QThread> #include <QtCore> class Professor004 : public QThread { public: Professor004(); void run(); }; #endif // PROFESSOR004_H
源文件:
#include "professor004.h" /**************** proferssor001---->A proferssor002---->B proferssor003--->C proferssor004---->D proferssor005---->E 资源夺取: A--->1/5 B--->2/1 C--->3/2 D--->4/3 E--->5/4 ******************/ extern QMutex mutex01; extern QMutex mutex02; extern QMutex mutex03; extern QMutex mutex04; extern QMutex mutex05; extern int n1; extern int n2; extern int n3; extern int n4; extern int n5; extern QWaitCondition waitcondition001; extern QWaitCondition waitcondition002; extern QWaitCondition waitcondition003; extern QWaitCondition waitcondition004; extern QWaitCondition waitcondition005; Professor004::Professor004() { } void Professor004::run() { while(1) { mutex04.lock(); mutex03.lock(); while((n4+n3)<2) { mutex03.unlock(); qDebug()<<"proferssor004哲学家没左边筷子~"; mutex04.unlock(); qDebug()<<"proferssor004哲学家没右边筷子~"; qDebug()<<"proferssor004----哲学家開始歇息...."; waitcondition004.wait(&mutex04); waitcondition003.wait(&mutex03); continue ; } qDebug()<<"proferssor004----哲学家開始吃饭~"; n3--; n4--; mutex03.unlock(); n3=(n3+1); qDebug()<<"proferssor004----哲学家放下左边筷子~"; msleep(2); mutex04.unlock(); n4=(n4+1); qDebug()<<"proferssor004----哲学家放下右边筷子~"; msleep(2); } return ; }
线程5头文件:
#ifndef PROFESSOR005_H #define PROFESSOR005_H #include <QThread> #include <QtCore> class Professor005 : public QThread { public: Professor005(); void run(); }; #endif // PROFESSOR005_H
源文件:
#include "professor005.h" /**************** proferssor001---->A proferssor002---->B proferssor003--->C proferssor004---->D proferssor005---->E 资源夺取: A--->1/5 B--->2/1 C--->3/2 D--->4/3 E--->5/4 ******************/ extern QMutex mutex01; extern QMutex mutex02; extern QMutex mutex03; extern QMutex mutex04; extern QMutex mutex05; extern int n1; extern int n2; extern int n3; extern int n4; extern int n5; extern QWaitCondition waitcondition001; extern QWaitCondition waitcondition002; extern QWaitCondition waitcondition003; extern QWaitCondition waitcondition004; extern QWaitCondition waitcondition005; Professor005::Professor005() { } void Professor005::run() { while(1) { mutex05.lock(); mutex04.lock(); while((n4+n5)<2) { mutex04.unlock(); qDebug()<<"proferssor005哲学家没左边筷子~"; mutex05.unlock(); qDebug()<<"proferssor005哲学家没右边筷子~"; qDebug()<<"proferssor005====哲学家開始歇息...."; waitcondition005.wait(&mutex05); waitcondition004.wait(&mutex04); continue ; } qDebug()<<"proferssor005====哲学家開始吃饭~"; n4--; n5--; mutex04.unlock(); n4=(n4+1); qDebug()<<"proferssor005====哲学家放下左边筷子~"; msleep(2); mutex05.unlock(); n5=(n5+1); qDebug()<<"proferssor005====哲学家放下右边筷子~"; msleep(2); } return ; }
执行结果:
资源免费下载:http://download.csdn.net/detail/feng1790291543/7324039
以上是关于Qt入门实践Qt之哲学家问题(linux 多线程)的主要内容,如果未能解决你的问题,请参考以下文章