Here's the core of a C++ program simulating what we just described (click here for full program):
static const unsigned int kNumPhilosophers = 5; // must be 2 or greater
static const unsigned int kNumForks = kNumPhilosophers;
static const unsigned int kNumMeals = 3;
static mutex forks[kNumForks]; // forks modeled as mutexes
static void think(unsigned int id) {
cout << oslock << id << " starts thinking." << endl << osunlock;
sleep_for(getThinkTime());
cout << oslock << id << " all done thinking. " << endl << osunlock;
}
static void eat(unsigned int id) {
unsigned int left = id;
unsigned int right = (id + 1) % kNumForks;
forks[left].lock();
forks[right].lock();
cout << oslock << id << " starts eating om nom nom nom." << endl << osunlock;
sleep_for(getEatTime());
cout << oslock << id << " all done eating." << endl << osunlock;
forks[left].unlock();
forks[right].unlock();
}
static void philosopher(unsigned int id) {
for (unsigned int i = 0; i < kNumMeals; i++) {
think(id);
eat(id);
}
}
int main(int argc, const char *argv[]) {
thread philosophers[kNumPhilosophers];
for (unsigned int i = 0; i < kNumPhilosophers; i++)
philosophers[i] = thread(philosopher, i);
for (thread& p: philosophers)
p.join();
return 0;
}