A RetroSearch Logo

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

Search Query:

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

public member function

<future>

std::promise::set_value generic template (1)
void set_value (const T& val);void set_value (T&& val);
specializations (2)
void promise<R&>::set_value (R& val);   // when T is a reference type (R&)void promise<void>::set_value (void);   // when T is void

Set value

Stores val as the value in the shared state, which becomes ready.

If a future object that is associated to the same shared state is currently waiting on a call to future::get, it unblocks and returns val.

The member of the void specialization simply makes the shared state ready, without setting any value.



Parameters
val
The value for the shared state.

Return value none

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.
The shared state is modified as an atomic operation (causing no data races).

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:


This member function also throws if the constructor selected to copy/move val throws (for (1)) and -depending on the library implementation- may also throw to report other situations.

See also
promise::set_exception
Set exception (public member function)
promise::set_value_at_thread_exit
Set value at thread exit (public member function)
future::get
Get value (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