1. 程式人生 > >【Qt入門實踐】Qt之哲學家問題(linux 多線程)

【Qt入門實踐】Qt之哲學家問題(linux 多線程)

avi .cpp private 1=1 endif debug 問題 tel pub

轉載請註明出處: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 多線程)