How do I use shared_mutex and unique_lock?

The shared_mutex class in C++ allows multiple threads to read shared data concurrently while ensuring exclusive access for writing. This is particularly useful in scenarios where read-heavy workloads are common, as it strikes a balance between read and write access.

To manage access to a shared_mutex, you can use the unique_lock class, which provides a convenient way to lock and unlock the mutex in a scoped manner. This helps prevent deadlocks and ensures that the mutex is properly released when it goes out of scope.

Example of shared_mutex and unique_lock


#include <iostream>
#include <shared_mutex>
#include <thread>

std::shared_mutex sharedMutex;
int sharedCounter = 0;

void readCounter() {
    std::shared_lock<std::shared_mutex> lock(sharedMutex); // Shared lock for reading
    std::cout << "Current Counter: " << sharedCounter << std::endl;
}

void incrementCounter() {
    std::unique_lock<std::shared_mutex> lock(sharedMutex); // Unique lock for writing
    ++sharedCounter;
}

int main() {
    std::thread readers[5];
    for (int i = 0; i < 5; ++i) {
        readers[i] = std::thread(readCounter);
    }

    std::thread writer(incrementCounter);

    for (int i = 0; i < 5; ++i) {
        readers[i].join();
    }
    writer.join();

    return 0;
}
    

shared_mutex unique_lock C++ multithreading thread safety concurrent programming read-write locks