?? 哲學家2(vc).txt
字號:
CSyncObject * aStick[6];
CMultiLock * P[5];
UINT MyThreadProc( LPVOID pParam );
*****************************************************************
aStick[0]=new CMutex(FALSE,"Stick0",NULL);
aStick[1]=new CMutex(FALSE,"Stick1",NULL);
aStick[2]=new CMutex(FALSE,"Stick2",NULL);
aStick[3]=new CMutex(FALSE,"Stick3",NULL);
aStick[4]=new CMutex(FALSE,"Stick4",NULL);
aStick[5]=aStick[0]; //new CMutex(FALSE,"Stick0",NULL);
P[0]=new CMultiLock(&(aStick[0]),2,FALSE);
P[1]=new CMultiLock(&(aStick[1]),2,FALSE);
P[2]=new CMultiLock(&(aStick[2]),2,FALSE);
P[3]=new CMultiLock(&(aStick[3]),2,FALSE);
P[4]=new CMultiLock(&(aStick[4]),2,FALSE);
int i;
for(i=1;i<6;i++)
{ AfxBeginThread(MyThreadProc,&i,THREAD_PRIORITY_NORMAL,0, 0, NULL );
Sleep(200); }
cin.get();
*****************************************************************
UINT MyThreadProc( LPVOID pParam )
{
CString str1;
CString str[5];
int num,select,result;
num=*(int *)pParam;
CEvent threadstart;
str1.Format("%d",num);//*(int *)pParam);
threadstart.SetEvent();
do
{
str[0]="第"+str1+"個哲學家正在思考, 是否要吃飯 ";
MessageBox(0,str[0],str1+"哲學家", MB_OK|MB_ICONEXCLAMATION);
str[1]="第"+str1+"個哲學家想吃飯,要求使用筷子,";
MessageBox(0,str[1],str1+"哲學家", MB_OK|MB_ICONEXCLAMATION);
(* P[num-1]).Lock();
str[2]="第"+str1+"個哲學家正在使用筷子,是否吃完 ";
MessageBox(0,str[2],str1+"哲學家", MB_OK|MB_ICONEXCLAMATION);
str[3]="第"+str1+"個哲學家筷子使用完畢,準備通告 ";
MessageBox(0,str[3],str1+"哲學家", MB_OK|MB_ICONEXCLAMATION);
(* P[num-1]).Unlock();
str[4]="第"+str1+"個哲學家通告完畢, 是否繼續 ";
//select=MessageBox(0,str[4],str1+"哲學家", MB_OKCANCEL|MB_ICONQUESTION);
threadstart.Lock();
result=::WaitForSingleObject(threadstart.m_hObject,2000);
if (result ==WAIT_OBJECT_0)
select=0;
else
select=1;
}
while (select==1);
return 0;
}
*****************************************************************
UINT MyThreadProc1( LPVOID pParam )
{
CString str1;
CString str[5];
int num,select;
num=*(int *)pParam;
str1.Format("%d",num);//*(int *)pParam);
do
{
str[0]="第"+str1+"個哲學家正在思考, 是否要吃飯 ";
MessageBox(0,str[0],str1+"哲學家", MB_OK|MB_ICONEXCLAMATION);
//str[1]="第"+str1+"個哲學家想吃飯,要求使用筷子,想拿右筷子";
//MessageBox(0,str[1],str1+"哲學家", MB_OK|MB_ICONEXCLAMATION);
if (num!=5)
{
str[1]="第"+str1+"個哲學家想吃飯,要求使用筷子,想拿右筷子";
MessageBox(0,str[1],str1+"哲學家", MB_OK|MB_ICONEXCLAMATION);
(* aStick[num-1]).Lock();
MessageBox(0,"第"+str1+"個哲學家正拿著右筷子,正想拿左筷子",str1+"哲學家", MB_OK|MB_ICONEXCLAMATION);
(* aStick[num]).Lock();
}
else
{
str[1]="第"+str1+"個哲學家想吃飯,要求使用筷子,想拿左筷子";
MessageBox(0,str[1],str1+"哲學家", MB_OK|MB_ICONEXCLAMATION);
(* aStick[0]).Lock();
MessageBox(0,"第"+str1+"個哲學家正拿著左筷子,正想拿右筷子",str1+"哲學家", MB_OK|MB_ICONEXCLAMATION);
(* aStick[4]).Lock();
}
MessageBox(0,"第"+str1+"個哲學家拿到一雙筷子,正在吃飯!是否吃完?",str1+"哲學家", MB_ICONEXCLAMATION);
//Sleep(100);
//str[2]="第"+str1+"個哲學家正在使用筷子,是否吃完 ";
//MessageBox(0,str[2],str1+"哲學家", MB_OK|MB_ICONEXCLAMATION);
str[3]="第"+str1+"個哲學家筷子使用完畢,準備通告 ";
MessageBox(0,str[3],str1+"哲學家", MB_OK|MB_ICONEXCLAMATION);
(* aStick[num-1]).Unlock();
(* aStick[num%5]).Unlock();
str[4]="第"+str1+"個哲學家通告完畢, 是否繼續 ";
select=MessageBox(0,str[4],str1+"哲學家", MB_OKCANCEL|MB_ICONQUESTION);
}
while (select==1);
return 0;
}
*****************************************************************
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -