shared_mutex c++
Mutex(mutual exclusion): It provides the concurrent access to resource, it allows one thread at a time to access the data.
Please refer this article: mutex c++11
Problem statement:
We have many reader and one writer and we want parallel reading of shared data but if we use mutex then only one reader can access the shared data.
Solution:
Use share mutex
Now shared mutex is the mutex that allow many reader to access the data in read only mode in parallel, in other word reader will acquire the mutex in shared mode but other reader can get the access to read it However writer will acquire the mutex in exclusive mode(that mean it will not allow other reader to access to read the data)
Header file:
#include<shared_mutex>
shared_lock c++
This lock helps us to acquire the shared mutex, it locks the associated shared mutex in shared mode (to lock it in exclusive mode, std::unique_lock can be used).
Source code of shared mutex with shared lock c++:
/* Program: Shared Mutex with shared Lock Author: Alpha Master Date: 16 Sep 2023 */ #include<iostream> #include<thread> #include<shared_mutex> #include<chrono> std::shared_mutex gSMtx; int gCount = 0; bool gdon = true; void readerThread1() { while(1) { std::shared_lock lck{gSMtx}; std::cout<<"In Reader First:"<<gCount<<std::endl; std::this_thread::sleep_for(std::chrono::seconds(2)); } } void readerThread2() { while(1) { std::shared_lock lck{gSMtx}; std::cout<<"In Reader Second:"<<gCount<<std::endl; std::this_thread::sleep_for(std::chrono::seconds(2)); } } void writer() { while(1) { std::lock_guard<std::shared_mutex> lgd(gSMtx); gCount++; std::cout<<"In Writer thread:"<<gCount<<std::endl; std::this_thread::sleep_for(std::chrono::seconds(2)); } } int main() { std::cout<<"In Main"<<std::endl; std::thread T1(writer); std::thread T2(readerThread1); std::thread T3(readerThread2); T1.join(); T2.join(); T3.join(); return 0; }
Output:
InĀ below output, you can see threads are reading in parallel.
In Main In Writer thread:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Reader First:1 In Reader Second:1 In Writer thread:2 In Reader First:2 In Reader Second:2 In Reader First:2 In Reader Second:2 In Writer thread:3 In Reader First:3 In Reader Second:3 In Reader First:3 In Reader Second:3 In Reader First:3 In Reader Second:3 In Reader First:3 In Reader Second:3 In Reader First:3 In Reader Second:3 In Reader First:3 In Reader Second:3 In Reader First:3 In Reader Second:3 In Reader First:3 In Reader Second:3