Namespaces
Variants

std::future<T>:: wait_until

From cppreference.net

Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(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
template < class Clock, class Duration >
std:: future_status wait_until ( const std:: chrono :: time_point < Clock,Duration > & timeout_time ) const ;
(C++11 이후)

wait_until 는 결과가 사용 가능해질 때까지 대기합니다. 이는 지정된 timeout_time 에 도달하거나 결과가 사용 가능해질 때까지 차단되며, 먼저 발생하는 조건에 따라 반환됩니다. 반환 값은 wait_until 이 반환된 이유를 나타냅니다.

만약 미래가 지연 평가를 사용한 async 호출의 결과인 경우, 이 함수는 대기하지 않고 즉시 반환됩니다.

이 함수를 호출하기 전에 valid() false 인 경우, 또는 Clock Clock 요구 사항을 충족하지 않는 경우 동작은 정의되지 않습니다. std:: chrono :: is_clock_v < Clock > false 인 경우 프로그램의 형식이 올바르지 않습니다. (C++20부터)

목차

매개변수

timeout_time - 블록이 최대한 지속될 시간 포인트

반환값

상수 설명
future_status::deferred 공유 상태가 지연 평가를 사용하는 지연된 함수를 포함하므로, 결과는 명시적으로 요청될 때만 계산됨
future_status::ready 결과가 준비됨
future_status::timeout 타임아웃이 만료됨

예외

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

참고 사항

구현체들은 호출 전에 valid ( ) == false 인 경우를 탐지하고 future_errc::no_state 오류 조건을 가진 std::future_error 를 throw하도록 권장됩니다.

표준은 timeout_time 에 연결된 클럭을 사용하여 시간을 측정할 것을 권장합니다; 해당 클럭이 단조 클럭일 필요는 없습니다. 클럭이 불연속적으로 조정되는 경우 이 함수의 동작에 대한 보장은 없지만, 기존 구현들은 timeout_time Clock 에서 std::chrono::system_clock 으로 변환하여 POSIX pthread_cond_timedwait 에 위임함으로써 시스템 클럭 조정은 준수하지만 사용자가 제공한 Clock 조정은 준수하지 않습니다. 어떤 경우든, 이 함수는 스케줄링 또는 자원 경합 지연으로 인해 timeout_time 이 경과한 이후까지 더 오래 대기할 수도 있습니다.


예제

#include <chrono>
#include <future>
#include <iostream>
#include <thread>
int main()
{
    std::chrono::system_clock::time_point two_seconds_passed
        = std::chrono::system_clock::now() + std::chrono::seconds(2);
    // 1초 후에 완료되는 future 생성
    std::promise<int> p1;
    std::future<int> f_completes = p1.get_future();
    std::thread([](std::promise<int> p1)
                { 
                    std::this_thread::sleep_for(std::chrono::seconds(1)); 
                    p1.set_value_at_thread_exit(9); 
                }, 
                std::move(p1)
    ).detach();
    // 5초 후에 완료되는 future 생성
    std::promise<int> p2;
    std::future<int> f_times_out = p2.get_future();
    std::thread([](std::promise<int> p2)
                { 
                    std::this_thread::sleep_for(std::chrono::seconds(5)); 
                    p2.set_value_at_thread_exit(8); 
                }, 
                std::move(p2)
    ).detach();
    std::cout << "2초 동안 대기 중..." << std::endl;
    if (std::future_status::ready == f_completes.wait_until(two_seconds_passed))
        std::cout << "f_completes: " << f_completes.get() << "\n";
    else
        std::cout << "f_completes가 완료되지 않았습니다!\n";
    if (std::future_status::ready == f_times_out.wait_until(two_seconds_passed))
        std::cout << "f_times_out: " << f_times_out.get() << "\n";
    else
        std::cout << "f_times_out가 완료되지 않았습니다!\n";
    std::cout << "완료!\n";
}

가능한 출력:

Waiting for 2 seconds...
f_completes: 9
f_times_out did not complete!
Done!

참고 항목

결과가 사용 가능해질 때까지 대기
(public member function)
결과를 대기하며, 지정된 시간 동안 사용 불가능한 경우 반환
(public member function)