【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++ 的 ...
随机推荐
- spring cloud Bug之was unable to refresh its cache! status = Cannot execute request on any known server
可能原因: 1.application.yml server: port: 10001spring: application: name: microservice-consumer-movieeur ...
- Java权限管理(授权与认证)
CRM权限管理 有兴趣的同学也可以阅读我最近分享的:Shiro框架原理分析 (PS : 这篇博客里面介绍了使用Shiro框架的方式实现权限管理) https://www.cnblogs.com/y ...
- mac 解决mysql启动不了
mac升级了mysql,没有替换原来的链接的情况下, 启动mysql restart 会失败 解决方法,找到mysql真正的安装目录 mysql.server start
- Qt 编写应用支持多语言版本--一个GUI应用示例
简介 上一篇博文已经说过如何编写支持多语言的Qt 命令行应用,这一篇说说Qt GUI 应用多语言支持的坑. 本人喜欢用代码来写布局,而不是用 Qt Designer 来设计布局,手写布局比 Qt De ...
- pcb中几个层的解释
阻焊层(Solder Mask):又称为绿油层,是PCB的非布线层,用于制成丝网漏印板,将不需要焊接的地方涂一层阻焊物质,防止焊接PCB时焊锡在高温下的流动性.在阻焊层上预留的焊盘大小,要比实际焊盘大 ...
- jQuery调用WCF 说明
在项目中用过一些WCF的技术这篇文章是对以前用过的一点东西的一个梳理 一,webconfig的配置除了一般的配置外,与WCF相关的配置如下 <system.serviceModel> ...
- 每天学点Python之collections
每天学点Python之collections 内容摘抄自:<python大法好>的每天学点Python之collections collections模块在内置数据类型(dict.list ...
- 九度oj 题目1072:有多少不同的面值组合?(set集合)
题目1072:有多少不同的面值组合? 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3627 解决:1852 题目描述: 某人有8角的邮票5张,1元的邮票4张,1元8角的邮票6张,用这些邮 ...
- mysql replication driver 在jdk1.6下失效问题解决
mysql diver包里有relication driver,可以在jdbc层进行读写分离,主写从读默认的配置方式是指定driver为ReplicationDriver,并改写jdbc url一起j ...
- POJ 1679 判最小生成树的不唯一性 或 利用次小生成树求解
题目大意: 给定一个无向图,寻找它的最小生成树,如果仅有一种最小生成树,输出所有边的和,否则输出unique! 根据kruscal原理来说,每次不断取尽可能小的边不断添加入最小生成树中,那么可知如果所 ...