std::this_t...d::sleep_for(3)
β¨ NAME β¨
std::this_thread::sleep_for β and bestie, the description is also literally std::this_thread::sleep_for, because the C++ Standard Libary woke up and chose minimalism that day π
π¦ SYNOPSIS π¦
okay so first things first: this little icon is defined in the header <thread>, and she's been around (since C++11), periodt. here's the whole shape of her:
template< class Rep, class Period >
void sleep_for( const std::chrono::duration<Rep, Period>& sleep_duration );
what she does is block the execution of the current thread for at least the specified sleep_duration. and yes, "at least" is highkey the key phrase here π
no cap, this function may block for longer than sleep_duration due to scheduling or resource contention delays. so if you asked for 2 seconds and the thread came back at 2.4, that's not a glitch, that's just the scheduler doing scheduler things π
also, 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 β which means if somebody messes with the system time while your thread is napping, the nap math can get weird. iconic, honestly π¦
π PARAMETERS π
sleep_durationβ the time duration to sleep. that's the whole input, bestie, she's not complicated π«Ά
πΈ RETURN VALUE πΈ
(none) β she returns absolutely nothing. she just goes to sleep. main character energy of doing the least and still being essential π
π EXCEPTIONS π
Any exception thrown by a clock, a time_point, or a duration during the execution can come through. BUT β and this is the slay part β clocks, time points, and durations provided by the standard library never throw. so if you're rolling with the standard stuff, you're chilling π§
π EXAMPLE π
// Run this code
#include <chrono>
#include <iostream>
#include <thread>
int main()
{
using namespace std::chrono_literals;
std::cout << "Hello waiter\n" << std::flush;
const auto start = std::chrono::high_resolution_clock::now();
std::this_thread::sleep_for(2000ms);
const auto end = std::chrono::high_resolution_clock::now();
const std::chrono::duration<double, std::milli> elapsed = end - start;
std::cout << "Waited " << elapsed << '\n';
}
π POSSIBLE OUTPUT π
Hello waiter
Waited 2000.13 ms
see that 2000.13? that's the "at least" energy in action, she took a whole extra 0.13 ms to wake up and honestly? relatable π΄
π«Ά SEE ALSO π«Ά
sleep_untilβ stops the execution of the current thread until a specified time point(C++11)(function) π₯
and that's the page, bestie β source is cppreference.com, version/date stamp 2024.06.10, and the page title is std::this_t...d::sleep_for(3) (yes the header abbreviates it like that, not me π). now go take a nap, you've earned it, ily π«Άπ
π¦ 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