std::this_t...:sleep_until(3)

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

πŸ’– std::this_thread::sleep_until πŸ’–

okay bestie, welcome to the std::this_thread::sleep_until page from the C++ Standard Libary manual, tagged std::this_t...:sleep_until(3). yes, the header squishes the title and β€œLibary” is typed like that β€” we are choosing peace and moving on 🫢✨


🌸 NAME 🌸

std::this_thread::sleep_until β€” yep, that’s the function, and that’s also literally the name. she’s std::this_thread::sleep_until, no notes πŸ’…


✨ Synopsis ✨

Defined in header <thread> 🧡

template< class Clock, class Duration >
void sleep_until( const std::chrono::time_point<Clock, Duration>& sleep_time );

This overload is (since C++11) β€” vintage-ish but still iconic.

It blocks the execution of the current thread until the specified sleep_time has been reached. periodt ⏰

Clock must meet the Clock requirements. The program is ill-formed if std::chrono::is_clock_v<Clock> is false. (since C++20) β€” so like, don’t try to pass a fake clock, bestie, it’s not gonna end well 😭

The standard recommends that the clock tied to sleep_time be used, in which case adjustments of the clock may be taken into account. So the duration of the block might be more or less than sleep_time - Clock::now() at the time of the call, depending on the direction of the adjustment and whether it is honored by the implementation. The function also may block until after sleep_time has been reached due to process scheduling or resource contention delays. she can oversleep, it’s giving β€œfive more minutes” energy πŸ’€πŸ›Œ


πŸŽ€ Parameters πŸŽ€

  • sleep_time β€” time to block until. that’s the whole assignment 🎯

πŸ’… Return value πŸ’…

(none) β€” literally nothing. she blocks, she’s done, she does not return a little gift for you. void queen πŸ‘‘


πŸ¦‹ Exceptions πŸ¦‹

Any exception thrown by Clock or Duration β€” and clocks and durations provided by the standard library never throw. so if something throws, it’s not the standard-library clock/duration being messy πŸ’…


🌈 Example 🌈

// Run this code β€” go off, bestie 🧚

// Run this code

#include <chrono>
#include <iostream>
#include <thread>

auto now() { return std::chrono::steady_clock::now(); }

auto awake_time()
{
    using std::chrono::operator""ms;
    return now() + 2000ms;
}

int main()
{
    std::cout << "Hello, waiter...\n" << std::flush;
    const auto start{now()};
    std::this_thread::sleep_until(awake_time());
    std::chrono::duration<double, std::milli> elapsed{now() - start};
    std::cout << "Waited " << elapsed.count() << " ms\n";
}

⭐ Possible output ⭐

Hello, waiter...
Waited 2000.17 ms

πŸ‘‘ See also πŸ‘‘

  • sleep_for β€” stops the execution of the current thread for a specified time duration (C++11) (function). her cousin who takes a duration instead of a time point 🫢
  • C documentation for thrd_sleep β€” the C side of the nap committee 😴

mwah πŸ’‹ that’s the page, bestie β€” source/version: http://cppreference.com, date 2024.06.10, page tag std::this_t...:sleep_until(3). now go take a nap (on purpose, not during code review) πŸŽ€πŸ’€

πŸ“¦ where this page comes from