A concurrency support library is designed to solve problems in modern C++ that arise with multi-thread development. 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 which is an instance of the class located in
<shared_mutex> header. In this post, we explain using shared mutexes locking in Modern C++.
Table of Contents
What is a mutex (mutual exclusion) 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
// do operations
What is a shared mutex in modern C++?
The shared mutex comes with C++14, it is an instance of the class located in
<shared_mutex> 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 how we can define a shared mutex by using
std::shared_mutex <mutex name>;
Is there an example about shared mutexes (std::shared mutex)?
Here is a simple example about
std::shared_mutex with its
unlock_shared() methods that comes with C++17.
// in a thread function
// do operations
How to use shared lock unlock mutexes methods?
unlock() methods as in
mutex type, In C++17, it is improved and supports the additional methods
try_lock_shared. Simply these are:
lock_shared method is used to block the calling thread until the thread obtains shared ownership of the mutex.
unlock_shared method is used to release shared ownership of the mutex held by the calling thread.
try_lock_shared method is used to obtain shared ownership of the mutex without blocking. Return type is can be
true if the method obtains ownership, or
false if it cannot.
Is there a full example of how to use shared mutexes (std::shared mutex) in C++?
Let’s assume that we have a global val and we read data by a
getv() and we write data by
putv() functions, and we run these functions in threads. Here is a full and simple example about shared mutexes (
void putv( int v )
std::this_thread::sleep_for(std::chrono::microseconds(2)); //some latency simulation
val = v;
void getv( int &v )
std::this_thread::sleep_for(std::chrono::microseconds(2)); // some latency simulation
v = val;
std::thread t1( getv , std::ref( readval ) );
std::thread t2( putv , 100);
std::cout << readval << std::endl;
std::cout << val << std::endl;
For more information about this feature, please see https://open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3659.html
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.