?? mutex.c
字號:
/********************************************************
* Dining.c modified by Jia-yuan Lin 05/17/04
********************************************************/
/////////////////////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include "MtVerify.h"
#include "dining.h"
//////////////////////////////////////////////////////////////
int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
extern HWND hWndMain; // Main Window Handle
extern BOOL bFastFood;
#define P_DELAY bFastFood ? rand()/25 : ((rand()%5)+1)*1000
int gDinerState[PHILOSOPHERS];
int gChopstickState[PHILOSOPHERS];
HANDLE gchopStick[PHILOSOPHERS]; // 1 chopstick between each philopher and his neighbor
#undef PostMessage
#define PostMessage SendMessage
DWORD WINAPI PhilosopherThread(LPVOID pVoid)
{
int iPhilosopher = (int) pVoid;
int iLeftChopstick = iPhilosopher;
int iRightChopstick = iLeftChopstick + 1;
DWORD result;
if (iRightChopstick > PHILOSOPHERS-1)
iRightChopstick = 0;
//Randomize the random number generator
srand( (unsigned)time( NULL ) * (iPhilosopher + 1) );
gDinerState[iPhilosopher] = RESTING; //wants chopsticks
Sleep(P_DELAY);
for(;;)
{
// Wait until both of my chopsticks are available
gDinerState[iPhilosopher] = WAITING; //wants chopsticks
PostMessage(hWndMain, WM_FORCE_REPAINT,0 ,0);
result = WaitForSingleObject(gchopStick[iLeftChopstick], INFINITE);
MTVERIFY(result == WAIT_OBJECT_0);
gChopstickState[iLeftChopstick] = iPhilosopher;
Sleep(P_DELAY/4);
gDinerState[iPhilosopher] = WAITING; //wants chopsticks
PostMessage(hWndMain, WM_FORCE_REPAINT,0 ,0);
result = WaitForSingleObject(gchopStick[iRightChopstick], INFINITE);
MTVERIFY(result == WAIT_OBJECT_0);
gChopstickState[iRightChopstick] = iPhilosopher;
// Philosopher can now eat a grain of rice
gDinerState[iPhilosopher] = EATING; //philosopher is eating
PostMessage(hWndMain, WM_FORCE_REPAINT,0 ,0);
Sleep(P_DELAY);
// Put down chopsticks
gDinerState[iPhilosopher] = RESTING; //philosopher is resting
gChopstickState[iRightChopstick] = UNUSED;
gChopstickState[iLeftChopstick] = UNUSED;
PostMessage(hWndMain, WM_FORCE_REPAINT,0 ,0);
MTVERIFY( ReleaseMutex(gchopStick[iLeftChopstick]) );
MTVERIFY( ReleaseMutex(gchopStick[iRightChopstick]) );
// Philosopher can now meditate
Sleep(P_DELAY);
} // end for
return 0;
}
//////////////////////////////////////////////////////////////
int Diner(void)
{
HANDLE hThread[PHILOSOPHERS];
DWORD dwThreadId;
int i;
for (i=0; i < PHILOSOPHERS; i++)
{
//Initialize the chopsitcks to unused
gChopstickState[i] = UNUSED;
// initialize the diner state table
gDinerState[i] = 0;
// The Philosophers prepare to eat
gchopStick[i] = CreateMutex(NULL, FALSE, NULL);
MTVERIFY(gchopStick[i] != NULL);
}
for (i = 0; i < PHILOSOPHERS; i++)
MTVERIFY( hThread[i] = CreateThread(NULL, 0, PhilosopherThread, (LPVOID) i, 0, &dwThreadId ));
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -