std::this_t...d::sleep_for(3)

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

✨ 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