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

📄 philosophyview.cpp

📁 经典的哲学家模拟算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:

		V(&chock[0]);
		philo[1].right=0;
		philo[1].status=1;
		Pause.SetEvent();

		V(&chair);
		c[1]=false;
		flag[1]=0;
		Pause.SetEvent();

		thinking(Param);
	}
	return 0;
}

UINT ThreadProc3(LPVOID Param)
{
	while(1)
	{
		::WaitForSingleObject(Apply.m_hObject,INFINITE);
		P(&chair);
		c[2]=true;
		flag[2]=1;
		Pause.SetEvent();

		P(&chock[2]);
		philo[2].left=1;
		philo[2].status=1;
		Pause.SetEvent();

		P(&chock[1]);
		philo[2].right=1;
		philo[2].status=2;
		Pause.SetEvent();

		eating(Param);

		V(&chock[2]);
		philo[2].left=0;
		philo[2].status=1;
		Pause.SetEvent();

		V(&chock[1]);
		philo[2].right=0;
		philo[2].status=1;
		Pause.SetEvent();

		V(&chair);
		c[2]=false;
		flag[2]=0;
		Pause.SetEvent();

		thinking(Param);
	}
	return 0;
}

UINT ThreadProc4(LPVOID Param)
{
	while(1)
	{
		::WaitForSingleObject(Apply.m_hObject,INFINITE);
		P(&chair);
		c[3]=true;
		flag[3]=1;
		Pause.SetEvent();

		P(&chock[3]);
		philo[3].left=1;
		philo[3].status=1;
		Pause.SetEvent();

		P(&chock[2]);
		philo[3].right=1;
		philo[3].status=2;
		Pause.SetEvent();

		eating(Param);

		V(&chock[3]);
		philo[3].left=0;
		philo[3].status=1;
		Pause.SetEvent();

		V(&chock[2]);
		philo[3].right=0;
		philo[3].status=1;
		Pause.SetEvent();

		V(&chair);
		c[3]=false;
		flag[3]=0;
		Pause.SetEvent();

		thinking(Param);
	}
	return 0;
}

UINT ThreadProc5(LPVOID Param)
{
	while(1)
	{
		::WaitForSingleObject(Apply.m_hObject,INFINITE);
		P(&chair);
		c[4]=true;
		flag[4]=1;
		Pause.SetEvent();

		P(&chock[4]);
		philo[4].left=1;
		philo[4].status=1;
		Pause.SetEvent();

		P(&chock[3]);
		philo[4].right=1;
		philo[4].status=2;
		Pause.SetEvent();

		eating(Param);

		V(&chock[4]);
		philo[4].left=0;
		philo[4].status=1;
		Pause.SetEvent();

		V(&chock[3]);
		philo[4].right=0;
		philo[4].status=1;
		Pause.SetEvent();

		V(&chair);
		c[4]=false;
		flag[4]=0;
		Pause.SetEvent();

		thinking(Param);
	}
	return 0;
}

//随机申请线程
UINT ThreadRand(LPVOID Param)
{
	CPhilosophyView *p=(CPhilosophyView *)Param;
	CDC *pDC=p->GetDC();
	CBrush lighton,lightoff,*old;
	lightoff.CreateSolidBrush(RGB(255,0,0));
	lighton.CreateSolidBrush(RGB(0,255,0));
	while(1)
	{
		if(!mode)
		{
			::Sleep(1000);
			int temp=rand();
			if(temp>26487)
			{
				old=pDC->SelectObject(&lighton);
				pDC->Ellipse(80,30,100,50);
				pDC->SelectObject(old);
				light=true;
				Apply.SetEvent();
			}
			else
			{
				old=pDC->SelectObject(&lightoff);
				pDC->Ellipse(80,30,100,50);
				pDC->SelectObject(old);
				light=false;
			}
		}
		else
			Apply.SetEvent();
	}
	return 0;
}

//状态刷新线程
UINT ThreadRefresh(LPVOID Param)
{
	CString string[10],chocks[5],m_temp;
	CPhilosophyView *print=(CPhilosophyView *)Param;
	CDC *pdc=print->GetDC();
	while(1)
	{

		::WaitForSingleObject(Pause.m_hObject,INFINITE);
		stringchair.Format("%d",chair);
		pdc->TextOut(480,30,stringchair);
		if(!chock[0])
		{
			CPen *oldpen=pdc->SelectObject(&repen);
			pdc->MoveTo(mpos[0].x,mpos[0].y);
			pdc->LineTo(lpos[0].x,lpos[0].y);
			pdc->SelectObject(oldpen);
			
		}
		else
		{
			CPen *oldpen=pdc->SelectObject(&pen);
			pdc->MoveTo(mpos[0].x,mpos[0].y);
			pdc->LineTo(lpos[0].x,lpos[0].y);
			pdc->SelectObject(oldpen);
		}

		if(!chock[1])
		{
			CPen *oldpen=pdc->SelectObject(&repen);
			pdc->MoveTo(mpos[1].x,mpos[1].y);
			pdc->LineTo(lpos[1].x,lpos[1].y);
			pdc->SelectObject(oldpen);
			
		}
		else
		{
			CPen *oldpen=pdc->SelectObject(&pen);
			pdc->MoveTo(mpos[1].x,mpos[1].y);
			pdc->LineTo(lpos[1].x,lpos[1].y);
			pdc->SelectObject(oldpen);
		}

		if(!chock[2])
		{
			CPen *oldpen=pdc->SelectObject(&repen);
			pdc->MoveTo(mpos[2].x,mpos[2].y);
			pdc->LineTo(lpos[2].x,lpos[2].y);
			pdc->SelectObject(oldpen);
			
		}
		else
		{
			CPen *oldpen=pdc->SelectObject(&pen);
			pdc->MoveTo(mpos[2].x,mpos[2].y);
			pdc->LineTo(lpos[2].x,lpos[2].y);
			pdc->SelectObject(oldpen);
		}

		if(!chock[3])
		{
			CPen *oldpen=pdc->SelectObject(&repen);
			pdc->MoveTo(mpos[3].x,mpos[3].y);
			pdc->LineTo(lpos[3].x,lpos[3].y);
			pdc->SelectObject(oldpen);
			
		}
		else
		{
			CPen *oldpen=pdc->SelectObject(&pen);
			pdc->MoveTo(mpos[3].x,mpos[3].y);
			pdc->LineTo(lpos[3].x,lpos[3].y);
			pdc->SelectObject(oldpen);
		}

		if(!chock[4])
		{
			CPen *oldpen=pdc->SelectObject(&repen);
			pdc->MoveTo(mpos[4].x,mpos[4].y);
			pdc->LineTo(lpos[4].x,lpos[4].y);
			pdc->SelectObject(oldpen);
			
		}
		else
		{
			CPen *oldpen=pdc->SelectObject(&pen);
			pdc->MoveTo(mpos[4].x,mpos[4].y);
			pdc->LineTo(lpos[4].x,lpos[4].y);
			pdc->SelectObject(oldpen);
		}

		if(c[0])
			pdc->Ellipse(pos[0].x+50,pos[0].y+8,pos[0].x+70,pos[0].y+28);
		else
		{
			CPen *oldpen=(CPen*)pdc->SelectStockObject(WHITE_PEN);
			pdc->Ellipse(pos[0].x+50,pos[0].y+8,pos[0].x+70,pos[0].y+28);
			pdc->SelectObject(oldpen);
		}
		if(c[1])
			pdc->Ellipse(pos[1].x+50,pos[1].y+8,pos[1].x+70,pos[1].y+28);
		else
		{
			CPen *oldpen=(CPen*)pdc->SelectStockObject(WHITE_PEN);
			pdc->Ellipse(pos[1].x+50,pos[1].y+8,pos[1].x+70,pos[1].y+28);
			pdc->SelectObject(oldpen);
		}
		if(c[2])
			pdc->Ellipse(pos[2].x+50,pos[2].y+8,pos[2].x+70,pos[2].y+28);
		else
		{
			CPen *oldpen=(CPen*)pdc->SelectStockObject(WHITE_PEN);
			pdc->Ellipse(pos[2].x+50,pos[2].y+8,pos[2].x+70,pos[2].y+28);
			pdc->SelectObject(oldpen);
		}
		if(c[3])
			pdc->Ellipse(pos[3].x-20,pos[3].y+8,pos[3].x-40,pos[3].y+28);
		else
		{
			CPen *oldpen=(CPen*)pdc->SelectStockObject(WHITE_PEN);
			pdc->Ellipse(pos[3].x-20,pos[3].y+8,pos[3].x-40,pos[3].y+28);
			pdc->SelectObject(oldpen);
		}
		if(c[4])
			pdc->Ellipse(pos[4].x-20,pos[4].y+8,pos[4].x-40,pos[4].y+28);
		else
		{
			CPen *oldpen=(CPen*)pdc->SelectStockObject(WHITE_PEN);
			pdc->Ellipse(pos[4].x-20,pos[4].y+8,pos[4].x-40,pos[4].y+28);
			pdc->SelectObject(oldpen);
		}

		if(!philo[(timer-2)%5].left && !philo[(timer-2)%5].right)
			if(flag[(timer-2)%5])
			{			
				pdc->DrawState(pos[(timer-2)%5],CSize(32,32),(HBITMAP)b_bitmap[0],0,(HBRUSH)0);
				philo[(timer-2)%5].status=0;
			}
			else
			{
				pdc->DrawState(pos[(timer-2)%5],CSize(32,32),(HBITMAP)b_bitmap[1],0,(HBRUSH)0);
				philo[(timer-2)%5].status=1;
			}
		if(philo[(timer-2)%5].left!=philo[(timer-2)%5].right)
			pdc->DrawState(pos[(timer-2)%5],CSize(32,32),(HBITMAP)b_bitmap[1],0,(HBRUSH)0);
		if(philo[(timer-2)%5].left && philo[(timer-2)%5].right)
			pdc->DrawState(pos[(timer-2)%5],CSize(32,32),(HBITMAP)b_bitmap[2],0,(HBRUSH)0);

		pdc->CloseFigure();
	}
	return 0;
}

// CPhilosophyView 消息处理程序

void CPhilosophyView::OnStartthread()
{
	// TODO: 在此添加命令处理程序代码
	begin=true;
	htime=AfxBeginThread(ThreadTimer,GetSafeHwnd());
	hWnd1=AfxBeginThread(ThreadProc1,(LPVOID)this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
	hWnd2=AfxBeginThread(ThreadProc2,(LPVOID)this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
	hWnd3=AfxBeginThread(ThreadProc3,(LPVOID)this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
	hWnd4=AfxBeginThread(ThreadProc4,(LPVOID)this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
	hWnd5=AfxBeginThread(ThreadProc5,(LPVOID)this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
	hrand=AfxBeginThread(ThreadRand,(LPVOID)this);
	AfxBeginThread(ThreadRefresh,(LPVOID)this);
	Pause.SetEvent();
}

void CPhilosophyView::OnTimer(UINT nIDEvent)
{
//	CString string;
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	string.Format("%d",timer);
	CPhilosophyView *print=(CPhilosophyView *)this;
	CDC *pdc=print->GetDC();
	CPen *oldpen;
	pdc->TextOut(300,30,string);
	switch(timer%5)
	{
	case 1:
		StartThread1.SetEvent();
		pdc->MoveTo(pos[0].x-5,pos[0].y);
		pdc->LineTo(pos[0].x-5,pos[0].y+32);
		pdc->MoveTo(pos[0].x+37,pos[0].y);
		pdc->LineTo(pos[0].x+37,pos[0].y+32);

		oldpen=(CPen *)pdc->SelectStockObject(WHITE_PEN);
		pdc->MoveTo(pos[4].x-5,pos[4].y);
		pdc->LineTo(pos[4].x-5,pos[4].y+32);
		pdc->MoveTo(pos[4].x+37,pos[4].y);
		pdc->LineTo(pos[4].x+37,pos[4].y+32);
		pdc->SelectObject(oldpen);
		break;
	case 2:
		StartThread2.SetEvent();
		pdc->MoveTo(pos[1].x-5,pos[1].y);
		pdc->LineTo(pos[1].x-5,pos[1].y+32);
		pdc->MoveTo(pos[1].x+37,pos[1].y);
		pdc->LineTo(pos[1].x+37,pos[1].y+32);

		oldpen=(CPen *)pdc->SelectStockObject(WHITE_PEN);
		pdc->MoveTo(pos[0].x-5,pos[0].y);
		pdc->LineTo(pos[0].x-5,pos[0].y+32);
		pdc->MoveTo(pos[0].x+37,pos[0].y);
		pdc->LineTo(pos[0].x+37,pos[0].y+32);
		pdc->SelectObject(oldpen);
		break;
	case 3:
		StartThread3.SetEvent();
		pdc->MoveTo(pos[2].x-5,pos[2].y);
		pdc->LineTo(pos[2].x-5,pos[2].y+32);
		pdc->MoveTo(pos[2].x+37,pos[2].y);
		pdc->LineTo(pos[2].x+37,pos[2].y+32);

		oldpen=(CPen *)pdc->SelectStockObject(WHITE_PEN);
		pdc->MoveTo(pos[1].x-5,pos[1].y);
		pdc->LineTo(pos[1].x-5,pos[1].y+32);
		pdc->MoveTo(pos[1].x+37,pos[1].y);
		pdc->LineTo(pos[1].x+37,pos[1].y+32);
		pdc->SelectObject(oldpen);
		break;
	case 4:
		StartThread4.SetEvent();
		pdc->MoveTo(pos[3].x-5,pos[3].y);
		pdc->LineTo(pos[3].x-5,pos[3].y+32);
		pdc->MoveTo(pos[3].x+37,pos[3].y);
		pdc->LineTo(pos[3].x+37,pos[3].y+32);

		oldpen=(CPen *)pdc->SelectStockObject(WHITE_PEN);
		pdc->MoveTo(pos[2].x-5,pos[2].y);
		pdc->LineTo(pos[2].x-5,pos[2].y+32);
		pdc->MoveTo(pos[2].x+37,pos[2].y);
		pdc->LineTo(pos[2].x+37,pos[2].y+32);
		pdc->SelectObject(oldpen);
		break;
	case 0:
		StartThread5.SetEvent();
		pdc->MoveTo(pos[4].x-5,pos[4].y);
		pdc->LineTo(pos[4].x-5,pos[4].y+32);
		pdc->MoveTo(pos[4].x+37,pos[4].y);
		pdc->LineTo(pos[4].x+37,pos[4].y+32);

		oldpen=(CPen *)pdc->SelectStockObject(WHITE_PEN);
		pdc->MoveTo(pos[3].x-5,pos[3].y);
		pdc->LineTo(pos[3].x-5,pos[3].y+32);
		pdc->MoveTo(pos[3].x+37,pos[3].y);
		pdc->LineTo(pos[3].x+37,pos[3].y+32);
		pdc->SelectObject(oldpen);
		break;
	}
	timer++;
	CView::OnTimer(nIDEvent);
}

void CPhilosophyView::OnStopthread()
{
	// TODO: 在此添加命令处理程序代码
	hWnd1->SuspendThread();
	hWnd2->SuspendThread();
	hWnd3->SuspendThread();
	hWnd4->SuspendThread();
	hWnd5->SuspendThread();
	hrand->SuspendThread();
	KillTimer(20);
}
void CPhilosophyView::OnOptions()
{
	// TODO: 在此添加命令处理程序代码
	if(begin)
		OnStopthread();
	COptionDlg dialog;
	int result=dialog.DoModal();
	if(result==IDOK)
	{
		CPhilosophyDoc *pDoc=GetDocument();
		ASSERT_VALID(pDoc);
		timespan=dialog.timerspan*1000;
		eatingtime=dialog.eatingtime*10;
		thinkingtime=dialog.thinkingtime*10;
		mode=dialog.mode;
	}
	if(begin)
		OnContinue();
}

void CPhilosophyView::OnContinue()
{
	// TODO: 在此添加命令处理程序代码
	hrand->ResumeThread();
	m_timer=SetTimer(20,timespan,NULL);
	hWnd1->ResumeThread();
	hWnd2->ResumeThread();
	hWnd3->ResumeThread();
	hWnd4->ResumeThread();
	hWnd5->ResumeThread();
}

⌨️ 快捷键说明

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