From the course: Parallel and Concurrent Programming with C++ Part 1

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Deadlock: C++ demo

Deadlock: C++ demo - C++ Tutorial

From the course: Parallel and Concurrent Programming with C++ Part 1

Start my 1-month free trial

Deadlock: C++ demo

- To demonstrate a deadlock, we've created this example program that simulates two dining philosophers thinking and eating sushi. The philosopher function on line nine takes two reference parameters named first chopstick and second chopstick, which indicate the order in which the philosopher will lock the two mutexes. The while loop on line 10 will continue to run as long as the sushi count variable is positive, which represents the amount of sushi left on the shared plate between the philosophers. We initialize the sushi count to 5,000 on line seven, so our two philosophers should both end up very well fed. Within the while loop, the philosopher will pick up and acquire a lock on their first chopstick followed by their second chopstick, at which point they're in the critical section. If there's still sushi left on the plate, they'll take a piece by decrementing the sushi count variable online 14, then finally unlock both…

Contents