std::counti...quire_until(3)

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

πŸ’– NAME πŸ’–

Okay so bestie, welcome to the std::counti...quire_until(3) page from the C++ Standard Libary manual. The star of the show is std::counting_semaphore::try_acquire_until β€” yes, that's the whole NAME, and the page absolutely repeats it because why not πŸ’….


✨ Synopsis ✨

Here's the signature, copied exactly. The (since C++20) annotation is called out separately because she's iconic:

template< class Clock, class Duration >
bool try_acquire_until( const std::chrono::time_point<Clock,
Duration>& abs_time );

(since C++20) β€” this one is a C++20 girlie, no cap.

What it does: it tries to atomically decrement the internal counter by 1 if it's greater than 0. If it's not, it blocks until the counter is greater than 0 and it can successfully decrement the internal counter, or until the abs_time time point has been passed. It's giving "wait for the right moment" energy 🫢.

And heads up: the program is ill-formed if std::chrono::is_clock_v<Clock> is false. Like, compiler says nope 😭.


🚦 Preconditions 🚦

Clock has to meet the Clock requirements. Periodt.


πŸŽ€ Parameters πŸŽ€

  • abs_time β€” the earliest time the function must wait until in order to fail.

🫢 Return value 🫢

true if it decremented the internal counter, otherwise false. Simple, clean, slay.


😭 Exceptions 😭

It may throw std::system_error or a timeout-related exception. So yeah, she can get dramatic.


πŸ“ Notes πŸ“

In practice, the function may take longer than abs_time to fail. Not her being late on purpose, it's just literally how it is πŸ™ˆ.


πŸ‘€ See also πŸ‘€

  • release β€” increments the internal counter and unblocks acquirers (public member function).
  • acquire β€” decrements the internal counter or blocks until it can (public member function).
  • try_acquire β€” tries to decrement the internal counter without blocking (public member function).
  • try_acquire_for β€” tries to decrement the internal counter, blocking for up to a duration time (public member function).

source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::counti...quire_until(3). bye bestie, go decrement those counters responsibly πŸ’…βœ¨πŸŒΈ

πŸ“¦ where this page comes from