std::shared_lock::lock(3)
π 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_errorwith an error code ofstd::errc::operation_not_permitted. - If the associated mutex is already locked by this
shared_lock(that is,owns_lockreturnstrue), you getstd::system_errorwith an error code ofstd::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 andlock()responsibly, and remember: if there's no mutex associated,std::errc::operation_not_permittedis 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms