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.
Shared mutex: C++ demo - C++ Tutorial
From the course: Complete Guide to Parallel and Concurrent Programming with C++
Shared mutex: C++ demo
- [Instructor] The shared_mutex class has been part of the standard library since C++17, To provide a mutex that can be acquired in a shared mode, in which multiple threads can share ownership of the mutex as well as an exclusive mode in which only one thread can have a lock on the mutex. To demonstrate using a shared mutex, this example program creates 10 threads to concurrently read what day it is from a shared calendar while two other threads update it. The calendar here is just an array of strings to represent the days of the week on line eight, as well as an integer to indicate which day today is on line nine. We've created a single regular mutex named marker on line 10, which all of the threads will use to enforce mutual exclusion when interacting with the today variable. There are two functions on lines 12 and 21 for the threads that will be reading the calendar value and those that will be writing to update it. They both use a for loop to perform their action of either reading…
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.