Methods Associated with std::shared_mutex
The std::shared_mutex contains several member methods that are required to perform different operations. Some of the commonly used functions are:
S. No. |
Function |
Description |
---|---|---|
1 |
lock() | Exclusively locks the mutex and blocks if it fails. |
2 |
unlock() | Unlock the exclusive lock. |
3 |
try_lock() | Tries to uniquely lock the mutex. This function doesn’t block and returns false if the mutex is already locked for exclusive access. |
4 |
lock_shared() | Gets the mutex to use a shared lock. |
5 |
unlock_shared() | Unlocks the shared lock for the current thread. |
6 |
try_lock_shared() | Tries for shared lock. This function doesn’t block and returns false if fails. |
std::shared_mutex in C++
In C++, std::mutex is a mechanism that locks access to the shared resource when some other thread is working on it so that errors such as race conditions can be avoided and threads can be synchronized. But in some cases, several threads need to read the data from shared resources at the same time. Here, the std::shared_mutex comes into play. In this article, we will discuss the std::shared_mutex, its associated methods, and how it is different from the std::mutex in C++.
Prerequisite: C++ Multithreading, std::mutex in C++.
Contact Us