Learn About Useful Shared Mutexes Locking In Modern C++
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
header. In this post, we explain using shared mutexes locking in Modern C++.
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 lock()
and unlock()
methods,
std::mutex mtx;
mtx.lock(); // do operations mtx.unlock();
|
What is a shared mutex in modern 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 how we can define a shared mutex by using std::shared_mutex
.
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 try_lock_shared()
and unlock_shared()
methods that comes with C++17.
std::shared_mutex sharedmutex;
// in a thread function sharedmutex.try_lock_shared(); // do operations sharedmutex.unlock_shared();
|
How to use shared lock unlock mutexes methods?
A shared_mutex
has lock()
and unlock()
methods as in mutex
type, In C++17, it is improved and supports the additional methods lock_shared
, unlock_shared
, and try_lock_shared
. Simply these are:
lock_shared
(C++17)
The lock_shared
method is used to block the calling thread until the thread obtains shared ownership of the mutex.
unlock_shared
(C++17)
The unlock_shared
method is used to release shared ownership of the mutex held by the calling thread.
try_lock_shared
(C++17)
The 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 (std::shared mutex
).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include #include #include #include
std::shared_mutex sharedmutex;
int val=500;
void putv( int v ) { sharedmutex.lock(); std::this_thread::sleep_for(std::chrono::microseconds(2)); //some latency simulation val = v; sharedmutex.unlock(); }
void getv( int &v ) { sharedmutex.lock_shared(); std::this_thread::sleep_for(std::chrono::microseconds(2)); // some latency simulation v = val; sharedmutex.unlock_shared(); }
int main() { int readval; std::thread t1( getv , std::ref( readval ) ); std::thread t2( putv , 100);
t1.join(); t2.join();
std::cout << readval << std::endl; std::cout << val << std::endl;
system(“pause”); return 0; }
|
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.