A RetroSearch Logo

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

Search Query:

Showing content from https://cplusplus.com/packaged_task::get_future below:

public member function

<future>

std::packaged_task::get_future
future<Ret> 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 packaged_task once its stored task is called.

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

After this function has been called, the packaged_task is expected to make its shared state ready at some point (by calling its stored task), 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 packaged_task.
Ret is the return type of the stored task (the first template parameter of packaged_task).

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
25
// packaged_task::get_future
#include <iostream>     // std::cout
#include <utility>      // std::move
#include <future>       // std::packaged_task, std::future
#include <thread>       // std::thread

// a simple task:
int triple (int x) { return x*3; }

int main ()
{
  std::packaged_task<int(int)> tsk (triple); // package task

  std::future<int> fut = tsk.get_future();   // get future

  std::thread(std::move(tsk),33).detach();   // spawn thread and call task

  // ...

  int value = fut.get();                     // wait for the task to complete and get result

  std::cout << "The triple of 33 is " << value << ".\n";

  return 0;
}

Output:


Data races The packaged_task object is modified.

Exception safetyBasic guarantee: if an exception is thrown, the packaged_task 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)
packaged_task::operator()
Call stored task (public member function)
packaged_task::valid
Check for valid shared state (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