Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the words of [futures.state] a <code>std::future</code> is an <em>asynchronous return object</em> ("an object that reads results from a shared state") and a <code>std::promise</code> is an <em>asynchronous provider</em> ("an object that provides a result to a shared state") i.e. a promise is the thing that you <em>set</em> a result on, so that you can <em>get</em> it from the associated future.</p> <p>The asynchronous provider is what initially creates the shared state that a future refers to. <code>std::promise</code> is one type of asynchronous provider, <code>std::packaged_task</code> is another, and the internal detail of <code>std::async</code> is another. Each of those can create a shared state and give you a <code>std::future</code> that shares that state, and can make the state ready.</p> <p><code>std::async</code> is a higher-level convenience utility that gives you an asynchronous result object and internally takes care of creating the asynchronous provider and making the shared state ready when the task completes. You could emulate it with a <code>std::packaged_task</code> (or <code>std::bind</code> and a <code>std::promise</code>) and a <code>std::thread</code> but it's safer and easier to use <code>std::async</code>.</p> <p><code>std::promise</code> is a bit lower-level, for when you want to pass an asynchronous result to the future, but the code that makes the result ready cannot be wrapped up in a single function suitable for passing to <code>std::async</code>. For example, you might have an array of several <code>promise</code>s and associated <code>future</code>s and have a single thread which does several calculations and sets a result on each promise. <code>async</code> would only allow you to return a single result, to return several you would need to call <code>async</code> several times, which might waste resources.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload