Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::shared_mutex — shared and exclusive locking

std::shared_mutex lets multiple threads hold shared ownership simultaneously or one thread hold exclusive ownership. Use std::shared_lock for reading and std::unique_lock for writing, following the same protocol for every access to the protected data.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <shared_mutex>
#include <mutex>

std::shared_mutex m;
// In separate scopes:
{ std::shared_lock<std::shared_mutex> read_lock(m); }
{ std::unique_lock<std::shared_mutex> write_lock(m); }

std::shared_mutex, declared in <shared_mutex>, is a non-recursive mutex. Shared mode permits multiple owners at once; exclusive mode excludes every other owner. The usual protocol is to read under shared ownership and modify under exclusive ownership. The mutex itself does not police access to variables: your code must consistently follow that protocol.

lock() and unlock() manage exclusive ownership, while lock_shared() and unlock_shared() manage shared ownership. The blocking operations wait until ownership can be acquired. try_lock() and try_lock_shared() attempt acquisition without waiting and return bool; they may fail spuriously, so false does not prove that another thread owns the mutex.

Prefer RAII to manual unlocking: use std::shared_lock<std::shared_mutex> from <shared_mutex> for readers and std::unique_lock<std::shared_mutex> from <mutex> for writers. These objects release an owned lock when their scope ends, including during exception propagation. Shared ownership does not provide the exclusivity needed for ordinary modification of shared data.

Do not attempt to acquire either kind of ownership again in a thread that already owns the same mutex: the behavior is undefined. This includes attempting exclusive acquisition without releasing shared ownership. Unlocking and relocking is not an atomic mode change—data can change in between. Do not destroy an owned mutex or let a thread terminate while it still owns the mutex.

The example separates writing from reading with join() and protects every access to value with the appropriate lock. The readers may execute concurrently, but need not; they store results in separate variables. Printing after both join() calls produces the deterministic result 42 42.

Examples

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

int main() {
    std::shared_mutex mutex;
    int value = 0;

    std::thread writer([&] {
        std::unique_lock<std::shared_mutex> lock(mutex);
        value = 42;
    });
    writer.join();

    int first = 0;
    int second = 0;
    std::thread reader1([&] {
        std::shared_lock<std::shared_mutex> lock(mutex);
        first = value;
    });
    std::thread reader2([&] {
        std::shared_lock<std::shared_mutex> lock(mutex);
        second = value;
    });
    reader1.join();
    reader2.join();

    std::cout << first << ' ' << second << '\n';
}

Result

42 42

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources