From the course: Complete Guide to Parallel and Concurrent Programming with C++
Unlock this course with a free trial
Join today to access over 24,800 courses taught by industry experts.
Abandoned lock: C++ demo - C++ Tutorial
From the course: Complete Guide to Parallel and Concurrent Programming with C++
Abandoned lock: C++ demo
- [Instructor] To demonstrate what happens if a lock gets abandoned, we'll use a modified version of the previous dining philosophers 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 mutexes 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 locks the mutex and then something happens in the critical section, that thread could finish before it gets a chance to release the lock. To simulate that happening, I'll add another If statement that checks to see if there are exactly 10 pieces of sushi left. And if so, the thread will print a message…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.