From the course: Parallel and Concurrent Programming with C++ Part 1

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Shared mutex: C++ demo

Shared mutex: C++ demo - C++ Tutorial

From the course: Parallel and Concurrent Programming with C++ Part 1

Start my 1-month free trial

Shared mutex: C++ demo

- [Narrator] The shared mutex class has been part of the standard library since C plus plus 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…

Contents