Difference between revisions of "cpp/thread/sleep until"
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh) |
Andreas Krug (Talk | contribs) m (fmt, {{c}}, ., headers sorted) |
||
(10 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
{{cpp/title|n=this_thread::|sleep_until}} | {{cpp/title|n=this_thread::|sleep_until}} | ||
{{cpp/thread/navbar}} | {{cpp/thread/navbar}} | ||
− | {{ddcl | header=thread | | + | {{ddcl|header=thread|since=c++11|1= |
template< class Clock, class Duration > | template< class Clock, class Duration > | ||
− | void sleep_until( const std::chrono::time_point<Clock,Duration>& sleep_time ); | + | void sleep_until( const std::chrono::time_point<Clock, Duration>& sleep_time ); |
}} | }} | ||
− | Blocks the execution of the current thread until specified {{ | + | Blocks the execution of the current thread until specified {{c|sleep_time}} has been reached. |
− | + | {{cpp/thread/block until|sleep_time}} | |
===Parameters=== | ===Parameters=== | ||
− | {{ | + | {{par begin}} |
− | {{ | + | {{par|sleep_time|time to block until}} |
− | {{ | + | {{par end}} |
===Return value=== | ===Return value=== | ||
Line 19: | Line 19: | ||
===Exceptions=== | ===Exceptions=== | ||
− | {{ | + | Any exception thrown by {{tt|Clock}} or {{tt|Duration}} (clocks and durations provided by the standard library never throw). |
+ | |||
+ | ===Example=== | ||
+ | {{example | ||
+ | |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"; | ||
+ | } | ||
+ | |p=true | ||
+ | |output= | ||
+ | Hello, waiter... | ||
+ | Waited 2000.17 ms | ||
+ | }} | ||
===See also=== | ===See also=== | ||
− | {{ | + | {{dsc begin}} |
− | {{ | + | {{dsc inc|cpp/thread/dsc sleep_for}} |
− | {{ | + | {{dsc see c|c/thread/thrd_sleep}} |
+ | {{dsc end}} | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Latest revision as of 10:30, 23 October 2023
Defined in header <thread>
|
||
template< class Clock, class Duration > void sleep_until( const std::chrono::time_point<Clock, Duration>& sleep_time ); |
(since C++11) | |
Blocks the execution of the current thread until specified sleep_time has been reached.
Clock
must meet the Clock requirements. The program is ill-formed if std::chrono::is_clock_v<Clock> is false.(since C++20)
The standard recommends that the clock tied to sleep_time be used, in which case adjustments of the clock may be taken into account. Thus, 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.
Contents |
[edit] Parameters
sleep_time | - | time to block until |
[edit] Return value
(none)
[edit] Exceptions
Any exception thrown by Clock
or Duration
(clocks and durations provided by the standard library never throw).
[edit] Example
#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
[edit] See also
(C++11) |
stops the execution of the current thread for a specified time duration (function) |
C documentation for thrd_sleep
|