std::shared...ry_lock_for(3)

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

🌸 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 ⭐

  • true if the ownership of the mutex has been acquired successfully
  • false otherwise

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_error with an error code of std::errc::operation_not_permitted.
  • If the mutex is already locked: std::system_error with an error code of std::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 associated TimedLockable mutex, returns if the mutex has been unavailable for the specified time duration (public member function of std::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