转载请注明出处: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 多线程)的更多相关文章

  1. Qt入门学习——Qt 5 帮助文档的使用

    Qt入门学习——Qt 5 帮助文档的使用 学习图形界面开发,肯定离不开帮助文档的使用,因为它不像 C 语言那样就那么几个函数接口,图形接口的接口可以用海量来形容,常用的我们可能能记住,其它的真的没有必 ...

  2. Qt入门——使用QT+VS2008开发windows应用程序

    1.文件->新建->项目 Qt4 Projects 右边已安装模板当中选择At Application. 确定 2.选择需要使用的QT库 下一步 3. “class name”:指定类的名 ...

  3. Qt入门之基础篇(1):Qt4及Qt5的下载与安装

    转载请注明出处:CN_Simo. 导语: Qt是一个跨平台的C++图形界面应用程序框架.它提供给开发者建立图形用户界面所需的功能,广泛用于开发GUI程序,也可用于开发非GUI程序.Qt很容易扩展,并且 ...

  4. Qt入门之基础篇 ( 一 ) :Qt4及Qt5的下载与安装

    转载请注明出处:CN_Simo. 导语: Qt是一个跨平台的C++图形界面应用程序框架.它提供给开发者建立图形用户界面所需的功能,广泛用于开发GUI程序,也可用于开发非GUI程序.Qt很容易扩展,并且 ...

  5. Qt入门(1)——初识Qt

    Qt是一个跨平台C++图形用户界面应用程序开发框架.它既可以开发GUI程序,也可用于开发非GUI程序,比如控制台工具和服务器.Qt是面向对象的框架,使用特殊的代码生成扩展(称为元对象编译器(Meta ...

  6. Qt入门之基础篇 ( 二 ) :Qt项目建立、编译、运行和发布过程解析

    转载请注明出处:CN_Simo. 题解: 本篇内容主讲Qt应用从创建到发布的整个过程,旨在帮助读者能够快速走进Qt的世界. 本来计划是讲解Qt源码静态编译,如此的话读者可能并不能清楚地知道为何要静态编 ...

  7. Qt入门之基础篇(三):掌握Qt4的静态编译基本方法

    转载载请注明出处:CN_Simo. 导语: 前两章都提到过“静态编译”(Static Compilation),在Windows下一次静态编译差不多需要长达三个小时才能完成,而且还非常容易由于各种原因 ...

  8. Qt Charts实践

    Qt Charts的横空出世标志着QWT,QCustomPlot .....时代的终结,让我们开始使用QtCharts吧 在Qt 5.7.0中已经集成了Qt Charts模块,需要在安装Qt的时候把C ...

  9. QT入门学习笔记1:为什么要选QT及QT软件下载

    为什么选择QT? Qt突出的优势: ◆ Qt 是基于 C++ 的一种语言扩展(Extention) C/C++ 目前还是一种很多人都在学习的语言. Qt的好处就在于Qt本身可以被称作是一种 C++ 的 ...

随机推荐

  1. [SQL]数据分析SQL笔试题

    SQL笔试题 1.请简单写出left join和join的用法区别(可举例说明): 2.求出订单表(order表)中每个客户(custid)的最近一次购买日期(要求:按custid降序排列,trans ...

  2. 关闭警告&关闭eslint

    1.main.js中添加 vue.config.productiontip = false  这样即可去除警告!  第一个除了那个配置意外,还需要将 NODE_ENV 设置为 production 参 ...

  3. css + 和 ~的区别

    <style type="text/css"> h1 + p { margin-top:50px; color:red; } </style> <p& ...

  4. 启动myeclipse弹窗Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance

    Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance(翻译:请允许Sub ...

  5. socket编程(Java实现)

    主要是前段时间学习的网络知识的基于TCP与UDP编程,Java实现简单的大小写字母的转化,该文主要参考: https://blog.csdn.net/yjp19871013/article/detai ...

  6. linux与linux之间共享目录

    1.安装必要的包 nfs-utils           rpcbind (nfs是基于sun公司的rpc通信实现的,所以要装rpcbind) 这2包,在服务端和客户端都需要安装,并启动服务. 启动 ...

  7. lnmp -memcached使用

    系统需求: CentOS/RHEL/Fedora/Debian/Ubuntu/Raspbian/Deepin Server/Aliyun/Amazon/Mint Linux发行版 需要5GB以上硬盘剩 ...

  8. selenium的三种等待

    1. 强制等待 最简单粗暴,sleep(xx),不管你浏览器是否加载完了,程序都得等待xx秒,时间一到,再继续执行下面的代码,作为调试很有用,有时候也可以在代码里这样等待,不过不建议总用这种等待方式, ...

  9. codeforce 810B Summer sell-off (贪心 排序)

    题意: 商店准备用n天售货(每天的货物都是一样的),第i天会卖ki件货物,并且会有li个顾客来买. 如果货物没卖完, 那么每个顾客一定会买一件. 如果货物有剩, 不会保存到第二天. 现在给定一个f, ...

  10. pip提示Did not provide a commend

    今天小编想要查看一下自己安装的pip版本,并且使用pip查看selenium版本等,结果在cmd输入pip,提示Did not provide a commend,如下所示: 在网上查询了很多方法,比 ...