What Are The Differences Between Mutex And Shared Mutex In C++?

The concurrency support library in modern C++ is designed to solve read and write data securely in thread operations that allow us to develop faster multi-thread apps. This library includes built-in support for threads (std::thread), atomic operations (std::atomic), mutual exclusion (std::mutex), condition variables (std::condition_variable), and many other features. In C++14, in addition to mutex, there is a shared mutex (std::shared_mutex) which is an instance of the class located in header. In this post, we explain a frequently asked mutex question in modern C++, what are the differences between mutex and shared_mutex?

What is a mutex (std::shared_mutex) in C++?

Mutual Exclusion is a property of concurrency control and in programming, the Mutual Exclusion is a data exclusion method to lock and unlock data that provides exclusive access to a resource. This is mostly needed when we use shared data in multi-thread and multi-task operations in parallel programming. In C++, we can use std::mutex to define mutex data variables to protect his shared data from being simultaneously accessed by multiple threads.

Here is an example of how we can use std::mutex with its lock() and unlock() methods,

Here are more details and examples about std::mutex.

What is a shared mutex (std::shared_mutex) in C++?

The shared mutex comes with C++14, it is an instance of the class located in header and used with the shared_mutex class name in mutual exclusion operations of threads. The shared_mutex class is a part of the thread support library. It is a synchronization primitive for the thread operations that can be used to protect shared data when multiple threads try to access.

Here is a simple example about std::shared_mutex with its try_lock_shared() and unlock_shared() methods that comes with C++17.

Here are more details and a full example about shared_mutex.

———- LINK TO Learn About Useful Shared Mutexes Locking In Modern C++ ——————

What are the differences between std::mutex and std::shared_mutex?

While the std::mutex guarantees exclusive access to some kind of critical resource, the shared_mutex class extends this feature by a shared and exclusive level of accesses.

The shared_mutex can be used in exclusive access level to prevent access of any other thread from acquiring the mutex, as in std::mutex. No matter if the other thread is trying to acquire shared or exclusive access.

The shared_mutex can be used in the shared access level to allow multiple threads to acquire the mutex, but all of them are only in shared mode. In thread operations, exclusive access is not granted until all of the previously shared holders have returned the mutex. As long as an exclusive request is waiting, new shared ones are queued to be granted after the exclusive access.

For more information about shared mutex feature, please see https://open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3659.html

WWhat Are The Differences Between Mutex And Shared Mutex In C++ C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.