Namespaces
Variants

std::this_thread:: sleep_until

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
sleep_until
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
헤더 파일에 정의됨 <thread>
template < class Clock, class Duration >
void sleep_until ( const std:: chrono :: time_point < Clock, Duration > & sleep_time ) ;
(C++11 이후)

현재 스레드의 실행을 지정된 sleep_time 이 도달할 때까지 차단합니다.

Clock Clock 요구 사항을 충족해야 합니다. std:: chrono :: is_clock_v < Clock > false 인 경우 프로그램의 형식이 올바르지 않습니다. (C++20부터)

표준은 sleep_time 에 연결된 클록을 사용할 것을 권장하며, 이 경우 클록의 조정이 고려될 수 있습니다. 따라서 블록의 지속 시간은 호출 시점의 sleep_time - Clock :: now ( ) 보다 길거나 짧을 수 있으며, 이는 조정 방향과 구현이 이를 준수하는지 여부에 따라 달라집니다. 또한 이 함수는 프로세스 스케줄링이나 자원 경합 지연으로 인해 sleep_time 이 경과한 이후까지 블록될 수 있습니다.

목차

매개변수

sleep_time - 차단할 시간

반환값

(없음)

예외

Clock 또는 Duration 에 의해 발생하는 모든 예외 (표준 라이브러리에서 제공하는 클록과 지속 시간은 절대 예외를 발생시키지 않음).

예제

#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";
}

가능한 출력:

Hello, waiter...
Waited 2000.17 ms

참고 항목

(C++11)
현재 스레드의 실행을 지정된 시간 동안 중지합니다
(함수)
C documentation for thrd_sleep