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

📄 dinphil.cpp

📁 D:jh哲学家吃饭DINPHIL.rar哲学家吃饭
💻 CPP
字号:
//dinphil.cpp


#include<windows.h>
#include<stdlib.h>
#include<iostream.h>

const INT numPhilosophers=3;
HANDLE chopsticks[numPhilosophers];
CHAR csStat[numPhilosophers+1];
CHAR pStat[numPhilosophers+1];

void PhilosopherThread(INT id)
{
 while (1)
 {
	 //thinking
   Sleep(GetTickCount() % 50);

   //hungry,so picks up chopsticks
   WaitForSingleObject(chopsticks[id],
	   INFINITE);
   csStat[id]='1';
   WaitForSingleObject(
	   chopsticks[(id+1)%numPhilosophers],
	   INFINITE);
   csStat[(id+1)%numPhilosophers]='1';
   pStat[id]='1';

   // eating
   cout << csStat << " " << pStat << endl;
   Sleep(GetTickCount() % 50);

   pStat[id]='0';
   // done eating
   ReleaseMutex(chopsticks[id]);
   csStat[id]='0';
   ReleaseMutex(
	   chopsticks[(id+1)%numPhilosophers]);
   csStat[(id+1)%numPhilosophers]='0';
 }
}

void main(void)
{
	HANDLE handles[numPhilosophers];
	DWORD threadID;
	INT i;

	csStat[numPhilosophers]='\0';
	pStat[numPhilosophers]='\0';

	//Create the chopstick mutexes
	for (i=0;i<numPhilosophers;i++)
		chopsticks[i]=CreateMutex(0,FALSE,0);

	//Create the philosopher threads
	for (i=0;i<numPhilosophers;i++)
	{
		//create the philosopher threads
		handles[i]=CreateThread(0,0,
			(LPTHREAD_START_ROUTINE) PhilosopherThread,
			(VOID *) i, 0, &threadID);
	}

	// wait for all threads to finish execution
	WaitForMultipleObjects(numPhilosophers, handles,
		TRUE,INFINITE);

	for (i=0;i<numPhilosophers;i++)
		CloseHandle(chopsticks[i]);

	cout << "Done" << endl;
}

⌨️ 快捷键说明

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