std::unique...:unique_lock(3)
π NAME π
std::unique_lock::unique_lock β okay so this is the constructor page for std::unique_lock::unique_lock, from the C++ Standard Library, bestie. Yes, the NAME section is literally the same thing twice and honestly? iconic.
π¦ SYNOPSIS π¦
Constructs a unique_lock, optionally locking the supplied mutex. Here are all the constructor flavors, no cap:
- (1) (since C++11) β
unique_lock() noexcept; - (2) (since C++11) β
unique_lock( unique_lock&& other ) noexcept; - (3) (since C++11) β
explicit unique_lock( mutex_type& m ); - (4) (since C++11) β
unique_lock( mutex_type& m, std::defer_lock_t t ) noexcept; - (5) (since C++11) β
unique_lock( mutex_type& m, std::try_to_lock_t t ); - (6) (since C++11) β
unique_lock( mutex_type& m, std::adopt_lock_t t ); - (7) (since C++11) β template constructor, vibe below:
template< class Rep, class Period >
unique_lock( mutex_type& m,
const std::chrono::duration<Rep, Period>&
timeout_duration );
- (8) (since C++11) β template constructor, vibe below:
template< class Clock, class Duration >
unique_lock( mutex_type& m,
const std::chrono::time_point<Clock, Duration>&
timeout_time );
β¨ DESCRIPTION β¨
Constructs a unique_lock, optionally locking the supplied mutex. Letβs break it down constructor by constructor, because every single one matters:
- Constructs a
unique_lockwith no associated mutex. - Move constructor. Initializes the
unique_lockwith the contents ofother. Leavesotherwith no associated mutex. - Constructs a
unique_lockwithmas the associated mutex. Additionally: locks the associated mutex by callingm.lock(). - Constructs a
unique_lockwithmas the associated mutex. Additionally: does not lock the associated mutex. - Constructs a
unique_lockwithmas the associated mutex. Additionally: tries to lock the associated mutex without blocking by callingm.try_lock().
π¨ The behavior is undefined ifMutexdoes not satisfyLockable.
- Constructs a
unique_lockwithmas the associated mutex. Additionally: assumes the calling thread already holds a non-shared lock (i.e., a lock acquired bylock,try_lock,try_lock_for, ortry_lock_until) onm.
π¨ The behavior is undefined if not so. Like, do not test this, bestie.
- Constructs a
unique_lockwithmas the associated mutex. Additionally: tries to lock the associated mutex by callingm.try_lock_for(timeout_duration). Blocks until specifiedtimeout_durationhas elapsed or the lock is acquired, whichever comes first. May block for longer thantimeout_duration.
π¨ The behavior is undefined ifMutexdoes not satisfyTimedLockable.
- Constructs a
unique_lockwithmas the associated mutex. Additionally: tries to lock the associated mutex by callingm.try_lock_until(timeout_time). Blocks until specifiedtimeout_timehas been reached or the lock is acquired, whichever comes first. May block for longer than untiltimeout_timehas been reached.
π¨ The behavior is undefined ifMutexdoes not satisfyTimedLockable.
πΈ PARAMETERS πΈ
otherβ anotherunique_lockto initialize the state withmβ mutex to associate with the lock and optionally acquire ownership oftβ tag parameter used to select constructors with different locking strategiestimeout_durationβ maximum duration to block fortimeout_timeβ maximum time point to block until
π EXAMPLE π
// Run this code
#include <iostream>
#include <mutex>
#include <thread>
#include <utility>
#include <vector>
std::mutex m_a, m_b, m_c;
int a, b, c = 1;
void update()
{
{ // Note: std::lock_guard or atomic<int> can be used instead
std::unique_lock<std::mutex> lk(m_a);
++a;
}
{ // Note: see std::lock and std::scoped_lock for details and alternatives
std::unique_lock<std::mutex> lk_b(m_b, std::defer_lock);
std::unique_lock<std::mutex> lk_c(m_c, std::defer_lock);
std::lock(lk_b, lk_c);
b = std::exchange(c, b + c);
}
}
int main()
{
std::vector<std::thread> threads;
for (unsigned i = 0; i < 12; ++i)
threads.emplace_back(update);
for (auto& i : threads)
i.join();
std::cout << a << "'th and " << a + 1 << "'th Fibonacci numbers: "
<< b << " and " << c << '\n';
}
π OUTPUT π
12'th and 13'th Fibonacci numbers: 144 and 233
Source, version, and date check, bestie: this page is from http://cppreference.com, version/date 2024.06.10, and the footer signature is std::unique...:unique_lock(3). Lock responsibly, slay, and bye πβ¨π
π¦ 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