std::shared_lock::lock(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::shared_lock::lock β€” C++ Standard Libary, bestie ✨

okay so welcome to the std::shared_lock::lock page from the C++ Standard Libary manual (yes that's how it's spelled on the tin, we don't fix it, we respect it 🫢). This is the whole entire page about one (1) member function that grabs a shared lock, and honestly? It ate.


🌸 NAME 🌸

std::shared_lock::lock β€” like, it's literally std::shared_lock::lock, that's the name, that's the vibe, no notes πŸ’…


✨ SYNOPSIS ✨

void lock();  (since C++14)

Locks the associated mutex in shared mode. Effectively calls mutex()->lock_shared().

That's it. That's the whole function. One line. Iconic behaviour, minimal footprint, main character energy πŸ‘‘


πŸŽ€ PARAMETERS πŸŽ€

(none)

Nothing. Zero. Empty set. Not a single argument to be found, and she still serves πŸ’–


πŸ¦‹ RETURN VALUE πŸ¦‹

(none)

Returns nothing. Gives you no data. Only vibes (and a lock). 🫑


πŸ”₯ EXCEPTIONS πŸ”₯

okay bestie this is the part where you pay attention, because it will throw at you and it will not be cute about it:

  • Any exceptions thrown by mutex()->lock_shared() β€” whatever that throws, this throws, fr fr.
  • If there is no associated mutex, you get std::system_error with an error code of std::errc::operation_not_permitted.
  • If the associated mutex is already locked by this shared_lock (that is, owns_lock returns true), you get std::system_error with an error code of std::errc::resource_deadlock_would_occur.
πŸ’€ Double-locking yourself is a you problem and the library knows, and it will snitch with std::errc::resource_deadlock_would_occur. Do not test her.

🌈 EXAMPLE 🌈

This section is incomplete Reason: show a meaningful use of shared_lock::lock

(we're keeping that note, it's part of the lore πŸ™ˆ β€” but there IS a runnable snippet below, so live a little)

// Run this code

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

std::string file = "Original content."; // Simulates a file
std::mutex output_mutex; // mutex that protects output operations.
std::shared_mutex file_mutex; // reader/writer mutex

void read_content(int id)
{
    std::string content;
    {
        std::shared_lock lock(file_mutex, std::defer_lock); // Do not lock it first.
        lock.lock(); // Lock it here.
        content = file;
    }
    std::lock_guard lock(output_mutex);
    std::cout << "Contents read by reader #" << id << ": " << content << '\n';
}

void write_content()
{
    {
        std::lock_guard file_lock(file_mutex);
        file = "New content";
    }
    std::lock_guard output_lock(output_mutex);
    std::cout << "New content saved.\n";
}

int main()
{
    std::cout << "Two readers reading from file.\n"
              << "A writer competes with them.\n";
    std::thread reader1{read_content, 1};
    std::thread reader2{read_content, 2};
    std::thread writer{write_content};
    reader1.join();
    reader2.join();
    writer.join();
    std::cout << "The first few operations to file are done.\n";
    reader1 = std::thread{read_content, 3};
    reader1.join();
}

Notice the move? std::shared_lock lock(file_mutex, std::defer_lock); β€” she's constructed but not locked yet, and then lock.lock(); does the actual locking. That's the whole point of this page, and it's lowkey a slay πŸŽ€

πŸ“ Possible output πŸ“

Two readers reading from file.
A writer competes with them.
Contents read by reader #1: Original content.
Contents read by reader #2: Original content.
New content saved.
The first few operations to file are done.
Contents read by reader #3: New content

(it says possible for a reason bestie β€” threading order is not a promise ✨)


πŸ‘‘ SEE ALSO πŸ‘‘

  • try_lock β€” tries to lock the associated mutex (public member function)
  • unlock β€” unlocks the associated mutex (public member function)

🧚 Category 🧚

  • Todo with reason

πŸ’œ k that's the page!! go forth and lock() responsibly, and remember: if there's no mutex associated, std::errc::operation_not_permitted is gonna find you πŸ’€

sign-off time ✨ β€” this lil beauty comes from http://cppreference.com, version 2024.06.10, dated 2024.06.10 (yes the date and the version match, that's not a typo, that's just how she rolls πŸ’…). byeee 🫢🌸

πŸ“¦ where this page comes from