Namespaces
Variants

std:: shared_future

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)
shared_future
(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
헤더 파일에 정의됨 <future>
template < class T > class shared_future ;
(1) (C++11부터)
template < class T > class shared_future < T & > ;
(2) (C++11부터)
template <> class shared_future < void > ;
(3) (C++11부터)

클래스 템플릿 std::shared_future 는 비동기 연산의 결과에 접근하기 위한 메커니즘을 제공하며, std::future 와 유사하지만 여러 스레드가 동일한 공유 상태를 대기할 수 있습니다. std::future 가 이동만 가능하여(따라서 하나의 인스턴스만 특정 비동기 결과를 참조할 수 있음) 다른 점은, std::shared_future 는 복사 가능하며 여러 공유 future 객체가 동일한 공유 상태를 참조할 수 있습니다.

여러 스레드가 동일한 공유 상태에 접근하는 것은 각 스레드가 자신만의 shared_future 객체 사본을 통해 접근할 경우 안전합니다.

목차

멤버 함수

future 객체를 생성합니다
(public member function)
future 객체를 소멸합니다
(public member function)
내용을 할당합니다
(public member function)
결과 얻기
결과를 반환합니다
(public member function)
상태
future가 공유 상태를 가지고 있는지 확인합니다
(public member function)
결과가 사용 가능해질 때까지 대기합니다
(public member function)
결과를 대기하며, 지정된 시간 제한 동안 사용할 수 없는 경우 반환합니다
(public member function)
결과를 대기하며, 지정된 시간 점에 도달할 때까지 사용할 수 없는 경우 반환합니다
(public member function)

예제

shared_future std::condition_variable::notify_all() 와 유사하게 여러 스레드에 동시에 신호를 보내는 데 사용될 수 있습니다.

#include <chrono>
#include <future>
#include <iostream>
int main()
{   
    std::promise<void> ready_promise, t1_ready_promise, t2_ready_promise;
    std::shared_future<void> ready_future(ready_promise.get_future());
    std::chrono::time_point<std::chrono::high_resolution_clock> start;
    auto fun1 = [&, ready_future]() -> std::chrono::duration<double, std::milli> 
    {
        t1_ready_promise.set_value();
        ready_future.wait(); // waits for the signal from main()
        return std::chrono::high_resolution_clock::now() - start;
    };
    auto fun2 = [&, ready_future]() -> std::chrono::duration<double, std::milli> 
    {
        t2_ready_promise.set_value();
        ready_future.wait(); // waits for the signal from main()
        return std::chrono::high_resolution_clock::now() - start;
    };
    auto fut1 = t1_ready_promise.get_future();
    auto fut2 = t2_ready_promise.get_future();
    auto result1 = std::async(std::launch::async, fun1);
    auto result2 = std::async(std::launch::async, fun2);
    // wait for the threads to become ready
    fut1.wait();
    fut2.wait();
    // the threads are ready, start the clock
    start = std::chrono::high_resolution_clock::now();
    // signal the threads to go
    ready_promise.set_value();
    std::cout << "Thread 1 received the signal "
              << result1.get().count() << " ms after start\n"
              << "Thread 2 received the signal "
              << result2.get().count() << " ms after start\n";
}

가능한 출력:

Thread 1 received the signal 0.072 ms after start
Thread 2 received the signal 0.041 ms after start

참고 항목

(C++11)
함수를 비동기적으로(새 스레드에서 potentially 실행) 실행하고 결과를 담을 std::future 를 반환함
(함수 템플릿)
(C++11)
비동기적으로 설정되는 값을 기다림
(클래스 템플릿)