Listing 1: Simple (and incorrect) solution to the DPP implemented with Win32 API. The explicit fork flags are superfluous in this solution because they are replaced by the internal counters of the mutexes
1 enum { N = 5}; // number of dining philosophers
2 static HANDLE fork[N]; // model forks as mutex semaphores
3
4 void think(long n) {. . . } // called when philosopher n thinks
5 void eat(long n) { . . . } // called when philosopher n eats
6
7 long WINAPI philosopher(long n) { // task for philosopher n
8 for (;;) { // philosopher task runs forever
9 think(n); // first the philosopher thinks for a while
10 // after thinking the philosopher becomes hungry...
11 WaitForSingleObject(fork[(n+1)%N], INFINITE); // get left fork
12 WaitForSingleObject(n], INFINITE); // get right fork
13 eat(n); // got both forks, can eat for a while
14 ReleaseMutex(fork[(n+1)%N]); // release left fork
15 ReleaseMutex(fork[n]); // release right fork
16 }
17 return 0;
18 }