Namespaces
Variants

std::this_thread:: sleep_for

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
sleep_for
(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 Rep, class Period >
void sleep_for ( const std:: chrono :: duration < Rep, Period > & sleep_duration ) ;
(C++11 이후)

현재 스레드의 실행을 지정된 sleep_duration 동안 최소한 블록합니다.

이 함수는 스케줄링 또는 자원 경합 지연으로 인해 sleep_duration 보다 더 오랫동안 블록될 수 있습니다.

표준은 지속 시간을 측정하는 데 스테디 클록을 사용할 것을 권장합니다. 구현체가 시스템 클록을 대신 사용하는 경우, 대기 시간이 클록 조정에 영향을 받을 수도 있습니다.

목차

매개변수

sleep_duration - 대기 시간

반환값

(없음)

예외

실행 중 clock , time_point , 또는 duration 에 의해 발생하는 모든 예외 (표준 라이브러리에서 제공하는 clocks, time points, durations은 예외를 발생시키지 않음).

예제

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

가능한 출력:

Hello waiter
Waited 2000.13 ms

참고 항목

지정된 시간 포인트까지 현재 스레드의 실행을 중지합니다
(function)