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

📄 eatingdlg.cpp

📁 根据哲学家就餐算法实现的程序。主要是解决可能出现的死锁问题。
💻 CPP
字号:
// EatingDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Eating.h"
#include "EatingDlg.h"
#include "math.h"


extern  CPoint ta5[5];
extern  CPoint tropic5[5];
extern  CPoint usetropic[5];
extern  int  radius;
extern  CPoint CenterPoint;


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About


int GetRandTime(void)
{
    srand((unsigned)time(NULL));
    return rand()%500;
}
UINT Eating(PVOID pParam)
{
    pThinkers m_ph=(pThinkers)pParam;
    int ID=m_ph->ThinkerCnt;
    while(WaitForSingleObject(m_ph->ThreadExit,0)!=WAIT_OBJECT_0)
    {
        m_ph->thinkerStatus[ID]=THINKING_STATUS;
        Sleep(800+20*(m_ph->m_speed)+GetRandTime());
        m_ph->thinkerStatus[ID]=HUNGRY_STATUS;
        ::InvalidateRect(m_ph->m_view,m_ph->thinkerRect[ID],TRUE);
        while(TRUE)
        {
            if(WaitForSingleObject(m_ph->torpics[(ID+THINKER_NUM-1)%THINKER_NUM],
                60+GetRandTime())==WAIT_OBJECT_0)
            {
                if(WaitForSingleObject(m_ph->torpics[ID],100)!=WAIT_OBJECT_0)
                    ReleaseMutex(m_ph->torpics[(ID+THINKER_NUM-1)%THINKER_NUM]);
                else
                    break;
            }
            Sleep(600+10*(m_ph->m_speed)+GetRandTime());
        }

        m_ph->thinkerStatus[ID]=EATING_STATUS;
        m_ph->tropicStatus[(ID+THINKER_NUM-1)%THINKER_NUM]=1;
        m_ph->tropicStatus[ID]=1;
        ::InvalidateRect(m_ph->m_view,m_ph->userRect[ID],TRUE);
        ::InvalidateRect(m_ph->m_view,m_ph->IdleRect[ID],TRUE);
        ::InvalidateRect(m_ph->m_view,m_ph->IdleRect[(ID+THINKER_NUM-1)%THINKER_NUM],TRUE);
        ::InvalidateRect(m_ph->m_view,m_ph->thinkerRect[ID],TRUE);
        Sleep(800+25*(m_ph->m_speed)+GetRandTime());
         m_ph->thinkerStatus[ID]=THINKING_STATUS;
         m_ph->tropicStatus[(ID+THINKER_NUM-1)%THINKER_NUM]=0;
        m_ph->tropicStatus[ID]=0;
        ::InvalidateRect(m_ph->m_view,m_ph->userRect[ID],TRUE);
        ::InvalidateRect(m_ph->m_view,m_ph->IdleRect[ID],TRUE);
        ::InvalidateRect(m_ph->m_view,m_ph->IdleRect[(ID+THINKER_NUM-1)%THINKER_NUM],TRUE);
        ::InvalidateRect(m_ph->m_view,m_ph->thinkerRect[ID],TRUE);
        ReleaseMutex(m_ph->torpics[(ID+THINKER_NUM-1)%THINKER_NUM]);
        ReleaseMutex(m_ph->torpics[ID]);
        
    }
    m_ph->ThinkerCnt--;
    m_ph->thinkerStatus[ID]=NORMAL_STATUS;
    ::InvalidateRect(m_ph->m_view,m_ph->thinkerRect[ID],TRUE);
    if( m_ph->ThinkerCnt==-1)
        ResetEvent(m_ph->ThreadExit);
    return 1;


}

void CEatingDlg::InitStruct()
{
    for(int i=0;i<THINKER_NUM;i++)
    {
        m_thinker.thinkerStatus[i]=NORMAL_STATUS;
        m_thinker.torpics[i]=CreateMutex(NULL,FALSE,NULL);
        m_thinker.tropicStatus[i]=0;
    }
    m_thinker.m_view=m_wndView.GetSafeHwnd();
    m_thinker.ThreadExit=CreateEvent(NULL,TRUE,FALSE,NULL);
}






class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEatingDlg dialog

CEatingDlg::CEatingDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEatingDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEatingDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEatingDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEatingDlg)
	DDX_Control(pDX, IDC_STATIC_VIEW, m_wndView);
	DDX_Control(pDX, IDC_SLIDER1, m_AdjustSpeed);
	DDX_Control(pDX, IDOK, m_okBtn);
	DDX_Control(pDX, IDC_BTN_STOP, m_stopBtn);
	DDX_Control(pDX, IDC_BTN_BEGIN, m_beginBtn);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEatingDlg, CDialog)
	//{{AFX_MSG_MAP(CEatingDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_BEGIN, OnBtnBegin)
	ON_BN_CLICKED(IDC_BTN_STOP, OnBtnStop)
	ON_WM_DESTROY()
	ON_NOTIFY(NM_OUTOFMEMORY, IDC_SLIDER1, OnOutofmemorySlider1)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER1, OnCustomdrawSlider1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEatingDlg message handlers

BOOL CEatingDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标
    InitStruct();
    m_thinker.m_speed=20;
    m_wndView.SetThinkersInfo(&m_thinker);
    m_wndView.SetGradientColor(RGB(0,0,0));
    font.CreatePointFont(160,(LPCTSTR)"华文行楷");
    m_wndView.SetFont(&font);
    m_AdjustSpeed.SetRange(0,100);
    m_AdjustSpeed.SetPos(80);
    m_AdjustSpeed.SetTicFreq(200);
    CRect rect;
    int i;
    radius=160;
    m_wndView.GetClientRect(&rect);
    CenterPoint=rect.CenterPoint();

    for(i=0;i<5;i++)
    {
        ta5[i]=CPoint(radius*cos((90+i*72)*3.1415926/180),
            radius*sin((90+i*72)*3.1415926/180));
        ta5[i].x+=CenterPoint.x;
        ta5[i].y=CenterPoint.y-ta5[i].y;
    }

    for(i=0;i<5;i++)
    {
        tropic5[i]=CPoint((radius-10)*cos((126+i*72)*3.1415926/180),
            (radius-10)*sin((126+i*72)*3.1415926/180));
        tropic5[i].x+=CenterPoint.x;
        tropic5[i].y=CenterPoint.y-tropic5[i].y;
    }

    for(i=0;i<5;i++)
    {
        usetropic[i]=CPoint((radius-10)*cos((90+i*72)*3.1415926/180),
            (radius-10)*sin((90+i*72)*3.1415926/180));
        usetropic[i].x+=CenterPoint.x;
        usetropic[i].y=CenterPoint.y-usetropic[i].y;
    }
    m_thinker.thinkerRect[0].SetRect(ta5[0].x,ta5[0].y-60,ta5[0].x,ta5[0].y-60);
    m_thinker.thinkerRect[0].InflateRect(60,20);
    m_thinker.thinkerRect[1].SetRect(ta5[1].x-85,ta5[1].y,ta5[0].x-85,ta5[0].y);
    m_thinker.thinkerRect[1].InflateRect(60,20);

        m_thinker.thinkerRect[2].SetRect(ta5[2].x-20,ta5[2].y+50,ta5[2].x-20,ta5[2].y+50);
    m_thinker.thinkerRect[2].InflateRect(60,20);

        m_thinker.thinkerRect[3].SetRect(ta5[3].x+10,ta5[3].y+50,ta5[3].x+10,ta5[3].y+50);
    m_thinker.thinkerRect[3].InflateRect(60,20);

        m_thinker.thinkerRect[4].SetRect(ta5[4].x+85,ta5[4].y,ta5[4].x+85,ta5[4].y);
    m_thinker.thinkerRect[4].InflateRect(60,20);
    m_stopBtn.EnableWindow(FALSE);
    m_HasInit=TRUE;
    return TRUE;

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CEatingDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CEatingDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CEatingDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CEatingDlg::OnBtnBegin() 
{
	int i;
    if(!m_HasInit)
        InitStruct();
    for(i=0;i<THINKER_NUM;i++)
    {
        m_thinker.ThinkerCnt++;
        AfxBeginThread(Eating,(PVOID)&m_thinker);
        Sleep(50);
    }
    m_stopBtn.EnableWindow(TRUE);
    m_beginBtn.EnableWindow(FALSE);
    m_okBtn.EnableWindow(FALSE);	
}

void CEatingDlg::OnBtnStop() 
{
	SetEvent(m_thinker.ThreadExit);
    BeginWaitCursor();
    while(WaitForSingleObject(m_thinker.ThreadExit,500)==WAIT_OBJECT_0)
    for(int i=0;i<THINKER_NUM;i++)
        CloseHandle(m_thinker.torpics[i]);

     CloseHandle(m_thinker.ThreadExit);
     EndWaitCursor();
      m_okBtn.EnableWindow(TRUE);
      m_beginBtn.EnableWindow(TRUE);
      m_stopBtn.EnableWindow(FALSE);
      m_HasInit=FALSE;
	
}

void CEatingDlg::OnOK() 
{
	
	CDialog::OnOK();
}

void CEatingDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	
}

void CEatingDlg::OnOutofmemorySlider1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

void CEatingDlg::OnCustomdrawSlider1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_thinker.m_speed=100-m_AdjustSpeed.GetPos();
	UpdateData(FALSE);
	*pResult = 0;
}

⌨️ 快捷键说明

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