日期:2014-05-16 浏览次数:21070 次
转载请注明出处:http://blog.csdn.net/feng1790291543
linux多线程实现哲学家问题,根据哲学家吃饭、拿筷子、放下筷子......
首先,主线程使用Qt下的GUI的简单一个按钮控制就行了
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; }
#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 proferssor00