Investigate a livelock scenario between two threads with an example C++ program. Resolve the livelock condition by implementing a random selection algorithm to choose which thread has priority to go first.
- [Instructor] To demonstrate a livelock scenario, … we'll modify our original dining philosophers example. … It creates two philosopher threads Barron and Olivia, … which each grab a different first and second chopstick, … and therefore have the potential to end up in a deadlock. … So, let's be clever and implement … our own deadlock avoidance algorithm. … I'll change the lock function on line 12 … for the second chopstick from a regular lock to try_lock. … And then I'll make that call to try_lock … the condition for an if statement. … If the second chopstick was already taken by another thread, … try_lock will return false. … In that case, this philosopher should politely put back … their first chopstick by unlocking it, … so another philosopher can take it. … Otherwise, if the current thread successfully locked … both chopsticks, it can continue on as normal, … taking a piece of sushi and then unlocking both chopsticks. … So, I'll enclose that within an else clause. … Switching over to a command prompt, I'll make, …
- Parallel computing architectures
- Shared vs. distributed memory
- Thread vs. process
- Execution scheduling
- The thread lifecycle in C++
- Mutual exclusion
- Locking in recursive and shared mutexes
- Acquiring a lock on a mutex with a try lock
- Resolving deadlock and livelock conditions
Skill Level Advanced
1. Parallel Computing Hardware
2. Threads and Processes
3. Mutual Exclusion
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.