std::unique...ry_lock_for(3)

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

🌸 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_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.

🧚 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 of std::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