⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simulation.h

📁 window下的多线程编程参考书。值得一读
💻 H
字号:
//
// FILE: Simulation.h
//
// Copyright (c) 1997 by Aaron Michael Cohen and Mike Woodring
//
/////////////////////////////////////////////////////////////////////////
#include "Mcl4Mfc.h"

#define NUMBER_PHILOSOPHERS     5

#define MINIMUM_THINKING_TIME   250
#define MINIMUM_EATING_TIME     250
#define MAXIMUM_THINKING_TIME   5000
#define MAXIMUM_EATING_TIME     5000

#define WM_DININGSIM_PHILOSOPHER_UPDATE     (WM_USER + 1024)
#define WM_DININGSIM_FORK_UPDATE            (WM_USER + 1025)

#define PHILOSOPHER_STATE_HUNGRY            0
#define PHILOSOPHER_STATE_THINKING          1
#define PHILOSOPHER_STATE_EATING            2

#define FORK_STATE_AVAILABLE                0
#define FORK_STATE_UNAVAILABLE              1

// CTable object definition...
class CTable {
private:
    // one mutex per fork...
    CMclMutex m_cmForks[NUMBER_PHILOSOPHERS];

    // table state...
    BOOL m_bContinue;

    // GUI window...
    HWND m_hwndGUI;

public:
    // constructor...
    CTable();

    // set the window which NotifyParent will send messages to...
    void SetHwnd(HWND hwnd);

    // take the forks needed by philosopher number nId...
    void TakeForks(int nId);

    // drop the forks taken by philosopher number nId...
    void DropForks(int nId);

    // philosophers call this to check if they
    // should continue...
    BOOL Continue(void);

    // put table in stop state...
    void Stop(void);
    
    // table is ready to run...
    void Run(void);

    // send messages to the m_hwndGUI window...
    void NotifyParent( UINT uMsg, WPARAM wParam, LPARAM lParam);
};

// CPhilosopher object definition...
class CPhilosopher : public CMclThreadHandler {
private:
    // this philsophers position at the table...
    int m_nId;

    // pointer to the table that all philosopher share...
    CTable *m_pTable;

public:
    // constructor...
    CPhilosopher( int nId, CTable *pt);

    // thread handler entry point...
    unsigned ThreadHandlerProc(void);

    // philosopher activities...
    void Philosophize(void);
    void Think(void);
    void Eat(void);
};

// CPhilosopherThread object definition...
class CPhilosopherThread : public CMcl4MfcWorkerThread {
private:
    // pointer to the internal thread handler object...
    CPhilosopher *m_pPhilosopher;
    
private:
    // private constructor for use by pseudo-virtual constructor...
    CPhilosopherThread( CPhilosopher *pPhilosopher);

public:
    virtual ~CPhilosopherThread();

    // pseudo-virtual constructor to create philosophers...
    static CPhilosopherThread *CreatePhilosopher( int nId, CTable *pt);
};

// CSimulation object definition...
class CSimulation {
private:
    // simulation-wide table object...
    CTable m_table;

    // philosopher thread objects...
    CMcl4MfcThreadAutoPtr m_cPhilosophers[NUMBER_PHILOSOPHERS];

public:
    // set the window which will receive simulation update messages...
    void SetHwnd(HWND hwnd);

    // start the simulation...
    void Run(void);

    // stop the simulation...
    void Stop(void);
};



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -