A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/future/promise/get_future/ below:

public member function

<future>

std::promise::get_future

Get future

Returns a future object associated with the object's shared state.

The future object returned can access the value or exception set on the shared state by the promise object once this is ready.

Only one future object can be retrieved for each promise shared state.

After this function has been called, the promise is expected to make its shared state ready at some point (by setting a value or an exception), otherwise it is automatically made ready on destruction containing an exception of type future_error (with a broken_promise error condition).



Parameters none

Return value A future object referring to the same shared state as this promise.
T is the type of the value (the template parameter of promise).

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// promise example
#include <iostream>       // std::cout
#include <functional>     // std::ref
#include <thread>         // std::thread
#include <future>         // std::promise, std::future

void print_int (std::future<int>& fut) {
  int x = fut.get();
  std::cout << "value: " << x << '\n';
}

int main ()
{
  std::promise<int> prom;                      // create promise

  std::future<int> fut = prom.get_future();    // engagement with future

  std::thread th1 (print_int, std::ref(fut));  // send future to new thread

  prom.set_value (10);                         // fulfill promise
                                               // (synchronizes with getting the future)
  th1.join();
  return 0;
}

Output


Data races The promise object is modified.

Exception safetyBasic guarantee: if an exception is thrown, the promise object is in a valid state.

This member function throws an exception on the following conditions:


Depending on the library implementation, this member function may also throw exceptions to report other situations (such as bad_alloc or system_error).

See also
future
Future (class template)
promise::set_value
Set value (public member function)
promise::set_exception
Set exception (public member function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4