Investigate atomic objects in C++ to contain a value that can be accessed from different threads without causing a data race. Observe that the mutex in the previous example C++ program can be replaced with an atomic object to achieve the same result.
- [Instructor] Using a lock to protect a shared variable … with mutual exclusion works. … But if you're only doing simple operations, … like incrementing a variable's value, … then the simpler solution … is to use C++ atomic types … which encapsulate a value and synchronize access to it … to prevent a data race. … If I scroll down this documentation page, … I see a variety of atomic types … including atomic Booleans, characters, … integers, long values, and so on. … Continuing down the page is a list of functions … of the atomic object … which implement many of the common operations. … To demonstrate using atomic objects, … I'm going to modify the previous example code … to demonstrate a data race … which has two parallel threads … incrementing a count variable 10 million times. … I should get an output of 20 million, … but since I'm using a regular integer … on line six, and I don't have any locks in place … to protect it with mutual exclusion. … When I run this program, … it has a data race that produces an incorrect result. …
- 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.