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.

Abandoned lock: C++ demo

Abandoned lock: C++ demo - C++ Tutorial

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

Start my 1-month free trial

Abandoned lock: C++ demo

- [Barron] To demonstrate what happens if a lock gets abandoned, we'll use a modified version of the previous dining philosopher's example that we used to demonstrate a deadlock. In this version, we still have two philosophers eating and thinking but we've consolidated the two mutexs into a single mutex simply named chopsticks. The philosophers will lock the chopstick mutex on line 11 before taking a bite of sushi and then unlocking it immediately afterwards on line 15. With only one mutex at play, the risk of deadlock is removed. So, if I run this program, the philosophers successfully take turns eating sushi until all of the pieces are gone. Now, if one of the philosopher threads lock the mutex, and then something happens in the critical section, that thread could finish before it gets the chance to release the lock. To simulate that happening, I'll add another if statement that checks to see if there are exactly…

Contents