std::this_t...:sleep_until(3)
π 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
- 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