|
|
Member functions | ||||
Getting the result | ||||
Setting the result | ||||
Non-member Functions | ||||
Helper Classes | ||||
Defined in header <future> | ||
template<class R>class promise; | (1) | (since C++11) |
template<class R>class promise<R&>; | (2) | (since C++11) |
template<>class promise<void>; | (3) | (since C++11) |
The class templatestd::promise
provides a facility to store a value or an exception that is later acquired asynchronously via astd::future object created by thestd::promise
object. Note that thestd::promise
object is meant to be used only once.
Each promise is associated with ashared state, which contains some state information and aresult which may be not yet evaluated, evaluated to a value (possibly void) or evaluated to an exception. A promise may do three things with the shared state:
The promise is the "push" end of the promise-future communication channel: the operation that stores a value in the shared statesynchronizes-with (as defined instd::memory_order) the successful return from any function that is waiting on the shared state (such asstd::future::get). Concurrent access to the same shared state may conflict otherwise: for example multiple callers ofstd::shared_future::get must either all be read-only or provide external synchronization.
Contents |
constructs the promise object (public member function)[edit] | |
destructs the promise object (public member function)[edit] | |
assigns the shared state (public member function)[edit] | |
swaps two promise objects (public member function)[edit] | |
Getting the result | |
returns afuture associated with the promised result (public member function)[edit] | |
Setting the result | |
sets the result to specific value (public member function)[edit] | |
sets the result to specific value while delivering the notification only at thread exit (public member function)[edit] | |
sets the result to indicate an exception (public member function)[edit] | |
sets the result to indicate an exception while delivering the notification only at thread exit (public member function)[edit] |
(C++11) | specializes thestd::swap algorithm (function template)[edit] |
specializes thestd::uses_allocator type trait (class template specialization)[edit] |
This example shows howpromise<int>
can be used as signals between threads.
#include <chrono>#include <future>#include <iostream>#include <numeric>#include <thread>#include <vector> void accumulate(std::vector<int>::iterator first,std::vector<int>::iterator last, std::promise<int> accumulate_promise){int sum=std::accumulate(first, last,0); accumulate_promise.set_value(sum);// Notify future} void do_work(std::promise<void> barrier){std::this_thread::sleep_for(std::chrono::seconds(1)); barrier.set_value();} int main(){// Demonstrate using promise<int> to transmit a result between threads.std::vector<int> numbers={1,2,3,4,5,6}; std::promise<int> accumulate_promise;std::future<int> accumulate_future= accumulate_promise.get_future();std::thread work_thread(accumulate, numbers.begin(), numbers.end(), std::move(accumulate_promise)); // future::get() will wait until the future has a valid result and retrieves it.// Calling wait() before get() is not needed// accumulate_future.wait(); // wait for resultstd::cout<<"result="<< accumulate_future.get()<<'\n'; work_thread.join();// wait for thread completion // Demonstrate using promise<void> to signal state between threads. std::promise<void> barrier;std::future<void> barrier_future= barrier.get_future();std::thread new_work_thread(do_work, std::move(barrier)); barrier_future.wait(); new_work_thread.join();}
Output:
result=21