std::shared_mutex
std::shared_mutex are usually used in situations when multiple readers can access the same resource at the same time without causing data races, but only one writer can do so. To protect shared data from being simultaneously accessed by multiple threads. #include<iostream> #include<mutex> #include<shared_mutex> #include<thread> class MutipleReaderSingleWriter { int get() const { std::shared_lock <std::shared_mutex> lock(mutex_); return _value; } void increment() { std::unique_lock <std::shared_mutex> lock(mutex_); _value++; } private: mutable std::shared_mutex mutex_; int _value = 0; }; int main() { MutipleReaderSingleWriter list; auto incrementAndPrint = [&list