Namespaces
Variants
Views
Actions

std::shared_future

From cppreference.com
< cpp‎ | thread
Revision as of 16:18, 5 June 2013 by Cubbi (Talk | contribs)

 
 
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
(C++11)
Generic lock management
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
shared_future
(C++11)
(C++11)
(C++11)
Safe Reclamation
(C++26)
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
Free functions for atomic operations
Free functions for atomic flags
 
 
Defined in header <future>
template< class T > class shared_future;
(1) (since C++11)
template< class T > class shared_future<T&>;
(2) (since C++11)
template<>          class shared_future<void>;
(3) (since C++11)

Contents

Member functions

constructs the future object
(public member function)
destructs the future object
(public member function)
assigns the contents
(public member function)
Getting the result
returns the result
(public member function) [edit]
State
checks if the future has a shared state
(public member function) [edit]
waits for the result to become available
(public member function) [edit]
waits for the result, returns if it is not available for the specified timeout duration
(public member function) [edit]
waits for the result, returns if it is not available until specified time point has been reached
(public member function) [edit]

Example

A shared_future may be used to signal multiple threads simultaneously, similar to std::condition_variable::notify_all()

#include <iostream>
#include <future>
#include <chrono>
 
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 = [&]() -> 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 = [&]() -> 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 result1 = std::async(std::launch::async, fun1);
    auto result2 = std::async(std::launch::async, fun2);
 
    // wait for the threads to become ready
    t1_ready_promise.get_future().wait();
    t2_ready_promise.get_future().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";
}

Possible output:

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