Namespaces
Variants

std::shared_future<T>:: wait

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
void wait ( ) const ;
(C++11부터)

결과를 사용할 수 있을 때까지 블록합니다. valid ( ) == true 호출 후에.

이 함수를 호출하기 전에 valid ( ) == false 인 경우 동작은 정의되지 않습니다.

목차

매개변수

(없음)

반환값

(없음)

예외

구현 정의 예외를 던질 수 있습니다.

참고 사항

구현체는 호출 전에 valid ( ) == false 인 경우를 탐지하고 std::future_error std::future_errc::no_state 오류 조건으로 던지도록 권장됩니다.

동일한 std::shared_future 에 대해 여러 스레드에서 wait를 호출하는 것은 안전하지 않습니다; 의도된 사용법은 동일한 공유 상태를 대기하는 각 스레드가 std::shared_future 의 복사본을 가지도록 하는 것입니다.

예제

#include <chrono>
#include <future>
#include <iostream>
#include <thread>
int fib(int n)
{
    if (n < 3)
        return 1;
    else
        return fib(n - 1) + fib(n - 2);
}
int main()
{
    std::shared_future<int> f1 = std::async(std::launch::async, []() { return fib(40); });
    std::shared_future<int> f2 = std::async(std::launch::async, []() { return fib(43); });
    std::cout << "waiting... " << std::flush;
    const auto start = std::chrono::system_clock::now();
    f1.wait();
    f2.wait();
    const auto diff = std::chrono::system_clock::now() - start;
    std::cout << std::chrono::duration<double>(diff).count() << " seconds\n";
    std::cout << "f1: " << f1.get() << '\n';
    std::cout << "f2: " << f2.get() << '\n';
}

가능한 출력:

waiting... 1.61803 seconds
f1: 102334155
f2: 433494437

참고 항목

결과를 기다리며, 지정된 시간 동안 사용 가능하지 않으면 반환합니다
(public member function)
결과를 기다리며, 지정된 시간 점에 도달할 때까지 사용 가능하지 않으면 반환합니다
(public member function)