【Qt入门实践】Qt之哲学家问题(linux 多线程)
转载请注明出处:http://blog.csdn.net/feng1790291543
linux多线程实现哲学家问题,依据哲学家吃饭、拿筷子、放下筷子......
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmVuZzE3OTAyOTE1NDM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
首先,主线程使用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 ;
}
执行结果:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmVuZzE3OTAyOTE1NDM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
资源免费下载:http://download.csdn.net/detail/feng1790291543/7324039
【Qt入门实践】Qt之哲学家问题(linux 多线程)的更多相关文章
- Qt入门学习——Qt 5 帮助文档的使用
Qt入门学习——Qt 5 帮助文档的使用 学习图形界面开发,肯定离不开帮助文档的使用,因为它不像 C 语言那样就那么几个函数接口,图形接口的接口可以用海量来形容,常用的我们可能能记住,其它的真的没有必 ...
- Qt入门——使用QT+VS2008开发windows应用程序
1.文件->新建->项目 Qt4 Projects 右边已安装模板当中选择At Application. 确定 2.选择需要使用的QT库 下一步 3. “class name”:指定类的名 ...
- Qt入门之基础篇(1):Qt4及Qt5的下载与安装
转载请注明出处:CN_Simo. 导语: Qt是一个跨平台的C++图形界面应用程序框架.它提供给开发者建立图形用户界面所需的功能,广泛用于开发GUI程序,也可用于开发非GUI程序.Qt很容易扩展,并且 ...
- Qt入门之基础篇 ( 一 ) :Qt4及Qt5的下载与安装
转载请注明出处:CN_Simo. 导语: Qt是一个跨平台的C++图形界面应用程序框架.它提供给开发者建立图形用户界面所需的功能,广泛用于开发GUI程序,也可用于开发非GUI程序.Qt很容易扩展,并且 ...
- Qt入门(1)——初识Qt
Qt是一个跨平台C++图形用户界面应用程序开发框架.它既可以开发GUI程序,也可用于开发非GUI程序,比如控制台工具和服务器.Qt是面向对象的框架,使用特殊的代码生成扩展(称为元对象编译器(Meta ...
- Qt入门之基础篇 ( 二 ) :Qt项目建立、编译、运行和发布过程解析
转载请注明出处:CN_Simo. 题解: 本篇内容主讲Qt应用从创建到发布的整个过程,旨在帮助读者能够快速走进Qt的世界. 本来计划是讲解Qt源码静态编译,如此的话读者可能并不能清楚地知道为何要静态编 ...
- Qt入门之基础篇(三):掌握Qt4的静态编译基本方法
转载载请注明出处:CN_Simo. 导语: 前两章都提到过“静态编译”(Static Compilation),在Windows下一次静态编译差不多需要长达三个小时才能完成,而且还非常容易由于各种原因 ...
- Qt Charts实践
Qt Charts的横空出世标志着QWT,QCustomPlot .....时代的终结,让我们开始使用QtCharts吧 在Qt 5.7.0中已经集成了Qt Charts模块,需要在安装Qt的时候把C ...
- QT入门学习笔记1:为什么要选QT及QT软件下载
为什么选择QT? Qt突出的优势: ◆ Qt 是基于 C++ 的一种语言扩展(Extention) C/C++ 目前还是一种很多人都在学习的语言. Qt的好处就在于Qt本身可以被称作是一种 C++ 的 ...
随机推荐
- mac 下安装python pil
mac:sudo brew install freetype sudo pip install pillow ubuntu: sudo apt-get install libfreetype6-dev ...
- 02Document Type Definition
Document Type Definition 1. Document Type Definition DTD(Document Type Definition)文件格式定义作用是给予文件一种格(T ...
- IIS更改根目录
服务器中打开IIS管理器,选择网站,再选展开后的站点,点击右边高级设置,最后更改弹框中的物理地址即可:
- JAVA基础——数据流
DataInputStream 类和DataOutputStream 类 在前面的学习中,我们知道数据流处理的数据都是指字节或字节数组,但实际上很多时候不是这样的,它需要数据流能直接读.写各种各样的j ...
- 如何HTML标签和JS中设置CSS3 var变量
一.HTML标签中设置CSS变量 如下: <div style="--color: #cd0000;"> <img src="mm.jpg" ...
- 笔试算法题(04):实现 string & memcpy & strcpy & strlen
出题:请实现给定String的类定义: 分析:注意检查标准类构造注意事项: 解题: #include <stdio.h> #include <string.h> /** * 检 ...
- Linux部署Web项目小记
1.安装Tomcat 官网下载 解压缩:tar -zxvf apache-tomcat-8.0.32.tar.gz 配置server.xml 连接池: <Executor name=" ...
- Go:数组
package main import "fmt" func main() { // 定义数组的方式 var array1 [3]int array2 := [3]int{} fm ...
- Linux htop工具使用详解【转】
原文地址: http://www.cnphp6.com/archives/65078 一.Htop的使用简介 大家可能对top监控软件比较熟悉,今天我为大家介绍另外一个监控软件Htop,姑且称之为to ...
- python 多线程并发threading & 任务队列Queue
https://docs.python.org/3.7/library/concurrency.htmlpython程序默认是单线程的,也就是说在前一句语句执行完之前后面的语句不能继续执行先感受一下线 ...