std::unique...ry_lock_for(3)
πΈ std::unique...ry_lock_for(3) πΈ
okay bestie, this page lives in the C++ Standard Libary manual β yes, that spelling is literally in the header, no cap. We're talking about std::unique_lock::try_lock_for, and the NAME line is a whole echo moment: std::unique_lock::try_lock_for - std::unique_lock::try_lock_for. iconic repetition, honestly π
β¨ NAME β¨
std::unique_lock::try_lock_for - std::unique_lock::try_lock_for
π Synopsis π
the signature is right here, and it's giving template energy:
template< class Rep, class Period >
bool try_lock_for( const std::chrono::duration<Rep, Period>&
(since C++11)
timeout_duration );
So what does it do? It tries to lock β i.e. take ownership of β the associated mutex. It blocks until the specified timeout_duration has elapsed or the lock is acquired, whichever comes first. On successful lock acquisition it returns true, otherwise it returns false. Effectively it calls mutex()->try_lock_for(timeout_duration).
heads up, bestie: this function may block for longer than timeout_duration due to scheduling or resource contention delays. The standard recommends that a steady clock is used to measure the duration. If an implementation uses a system clock instead, the wait time may also be sensitive to clock adjustments.
Also: std::system_error is thrown if there is no associated mutex or if the mutex is already locked by this std::unique_lock.
π Parameters π
timeout_durationβ maximum duration to block for. that's the max block window, periodt.
π₯ Return value π₯
true if the ownership of the mutex has been acquired successfully, false otherwise. simple as that π«Ά
π Exceptions π
- Any exceptions thrown by
mutex()->try_lock_for(timeout_duration). - If there is no associated mutex,
std::system_errorwith an error code ofstd::errc::operation_not_permitted. - If the mutex is already locked,
std::system_errorwith an error code ofstd::errc::resource_deadlock_would_occur.
π§ Example π§
okay so the example section is literally incomplete β the reason given is:
This section is incomplete
Reason: no example
no example, bestie. none. that's the whole tea π
π See also π
lockβ locks (i.e., takes ownership of) the associated mutex (public member function)try_lockβ tries to lock (i.e., takes ownership of) the associated mutex without blocking (public member function)try_lock_forβ tries to lock the associated mutex, for the specified duration (public member function ofstd::shared_lock<Mutex>)try_lock_untilβ tries to lock (i.e., takes ownership of) the associated TimedLockable mutex, returns if the mutex has been unavailable until specified time point has been reached (public member function)unlockβ unlocks (i.e., releases ownership of) the associated mutex (public member function)
π Category π
Todo no example
source/version + date shoutout, because footer details matter: http://cppreference.com, 2024.06.10, and the footer page label is std::unique...ry_lock_for(3). bye bestie πβ¨
π¦ 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