Investigate the use case for a recursive mutex when locks are nested by modifying the previous example C++ program to demonstrate mutual exclusion. Observe that the recursive mutex can be acquired multiple times by the same thread.
- [Instructor] To demonstrate using … a recursive mutex with C++, … we've modified the previous example that we used … to demonstrate a data race and mutual exclusion, … with two shoppers that concurrently increment … the number of items to buy. … In this version, we're counting the amount of garlic … and potatoes to buy with the variables that are initialized … on lines seven and eight. … There are two helper functions on lines 11 and 17, … called add_garlic and add_potato, … which increment the corresponding garlic count … or potato count variables, and each of those functions … lock and unlock the same mutex, named 'pencil', … to enforce mutual exclusion around those operations, … and prevent a data race. … The shopper function simply uses a for loop on line 24 … to execute those add_garlic and add_potato functions … 10,000 times each. … Down in the program's main function, … we create and start two shopper threads, … then, after they finish running, … we print out the amount of garlic and potatoes to buy. …
- 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.