std::shared...ry_lock_for(3)
πΈ std::shared_lock::try_lock_for πΈ
Okay bestie, so this page is literally about std::shared_lock::try_lock_for from the C++ Standard Library, and it's giving "I want shared access to the mutex but I'm not gonna wait forever" energy. No cap. π
β¨ Synopsis β¨
template< class Rep, class Period >
bool try_lock_for( const std::chrono::duration<Rep,Period>&
timeout_duration );
(since C++14)
π¦ DESCRIPTION π¦
So here's the vibe: this function tries to lock the associated mutex in shared mode. It blocks until either 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. Slay. π
Effectively it calls:
mutex()->try_lock_shared_for(timeout_duration)
Lowkey important: this function may block for longer than timeout_duration due to scheduling delays or resource contention delays. So don't come for it if it's a little extra. π
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. Not me warning you about clock drama. π
std::system_error is thrown if there is no associated mutex or if the mutex is already locked. So like, keep that in mind, bestie. π
Also: the behavior is undefined if Mutex does not meet the SharedTimedLockable requirements. That's literally in the original, don't test it, it's not a good time. Periodt.
π Parameters π
timeout_durationβ maximum duration to block for. That's it, that's the parameter. π
β Return value β
trueif the ownership of the mutex has been acquired successfullyfalseotherwise
Simple as that. Iconic. π
π₯ Exceptions π₯
- Any exceptions thrown by
mutex()->try_lock_shared_for(timeout_duration)β like, whatever that throws, you're catching it too. - 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.
So yeah, if you forgot to associate a mutex or it's already locked, it's throwing hands. π«Ά
π Example π
This section is incomplete Reason: no example
Literally no example yet. The cppreference girlies haven't gotten to it. We'll manifest it. π§ββοΈ
π See also π
try_lock_forβ attempts to lock (i.e., takes ownership of) the associatedTimedLockablemutex, returns if the mutex has been unavailable for the specified time duration (public member function ofstd::unique_lock<Mutex>)lockβ locks the associated mutex (public member function)try_lockβ tries to lock the associated mutex (public member function)try_lock_untilβ tries to lock the associated mutex, until a specified time point (public member function)unlockβ unlocks the associated mutex (public member function)
πΈ Category πΈ
- Todo no example
Okay that's the whole page, bestie. This was std::shared...ry_lock_for(3) from the C++ Standard Library manual, version/date 2024.06.10 as seen on http://cppreference.com. Go forth and try_lock_shared_for responsibly. Mwah! πβ¨ππ¦π
π¦ 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