<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>N2967: Resolving NB Comments and Issues on Futures</title>
  </head>

  <body>
<table>
  <tr>
    <td align="left">Document Number:</td>
    <td align="left">N2967=09-0157</td>
  </tr>
  <tr>
    <td align="left">Date:</td>
    <td align="left">2009-09-27</td>
  </tr>
  <tr>
    <td align="left">Project:</td>
    <td align="left">Programming Language C++</td>
  </tr>
</table>
<p>
Detlef Vollmann &lt;<a href="mailto:dv@vollmann.ch">dv@vollmann.ch</a>&gt;<br>
Anthony Williams &lt;<a href="mailto:anthony@justsoftwaresolutions.co.uk">anthony@justsoftwaresolutions.co.uk</a>&gt;<br>
</p>
<h1>N2967: Issues on Futures</h1>
<p>
This paper addresses a lot of editor's notes in N2798
and NB comments on clause 30.5 Futures (now 30.6).
</p>
<p>
This paper reflects the consensus after the discussion in Frankfurt.
After that there was more discussion on the reflector, but this paper
should provide a base for the discussion in Santa Cruz.
</p>

<table>
<tr>
 <th>NB comment</th>
 <th>LWG issue</th>
 <th>Short description</th>
 <th>Resolution</th>
</tr>
<tr>
 <td>UK 330</td>
 <td></td>
 <td>Reference to undefined <code>constructible_with_allocator_prefix</code></td>
 <td>References were removed</td>
</tr>

<tr>
 <td>JP 79</td>
 <td></td>
 <td>Concepts for <code>uses_allocator</code></td>
 <td>We don't have concepts now</td>
</tr>

<tr>
 <td>UK 331</td>
 <td>1160</td>
 <td><em>exposition only</em> for <code>error_code</code> constructor</td>
 <td>Constructor is now properly defined</td>
</tr>

<tr>
 <td>UK 332</td>
 <td></td>
 <td>Missing introduction to <code>unique_future</code></td>
 <td>An introduction was added</td>
</tr>

<tr>
 <td>UK 333</td>
 <td></td>
 <td>Conceptifying <code>*_future::get</code></td>
 <td>We don't have concepts now</td>
</tr>

<tr>
 <td>UK 334</td>
 <td>1047</td>
 <td>Missing blocking specification for <code>*_future::get</code></td>
 <td>Blocking was now specified</td>
</tr>

<tr>
 <td>UK 335</td>
 <td>1048</td>
 <td>Moved out state of a <code>unique_future</code> could not be detected</td>
 <td><code>valid()</code> was added</td>
</tr>

<tr>
 <td>UK 336</td>
 <td>1161</td>
 <td>Unnecessary <code>unique_future</code> limitations</td>
 <td>Default construction and move assignment were added</td>
</tr>

<tr>
 <td>JP 80</td>
 <td></td>
 <td>Typo in <code>*_future::wait_for</code></td>
 <td>Corrected</td>
</tr>

<tr>
 <td>UK 337</td>
 <td>1162</td>
 <td><code>shared_future</code> should support an efficient move constructor</td>
 <td>Move constructor was added</td>
</tr>

<tr>
 <td>UK 338</td>
 <td>1163</td>
 <td><code>shared_future</code> is inconsistent with <code>shared_ptr</code></td>
 <td>
<p>
Move constructor and move assignment and <code>atomic_future</code> were added.
</p>
<p>
The need for an <code>atomic_future</code> arose from the fact that
the move constructor and both assigments made the <code>shared_future</code>
much more error prone to use a single instance from multiple threads.
But such use is required when more than one thread scans a global list
of futures for results provided by worker threads.
</p>
 </td>
</tr>

<tr>
 <td>UK 339</td>
 <td>1049</td>
 <td><code>promise</code> move assignment goes into wrong direction</td>
 <td>Corrected</td>
</tr>

<tr>
 <td>UK 340</td>
 <td>1050</td>
 <td>Missing postcondition on <code>promise::get_future()</code></td>
 <td>Postcondition added</td>
</tr>

<tr>
 <td>UK 341</td>
 <td></td>
 <td>Wrong parameter type for <code>promise::swap()</code></td>
 <td>Corrected</td>
</tr>

<tr>
 <td>UK 341</td>
 <td></td>
 <td>Wrong parameter type for <code>promise::swap()</code></td>
 <td>Corrected</td>
</tr>

<tr>
 <td>UK 342</td>
 <td>1088</td>
 <td>Non-member <code>swap</code> for <code>promise::swap()</code></td>
 <td>Added</td>
</tr>

<tr>
 <td>UK 343</td>
 <td>1165</td>
 <td>Unneeded <code>promise</code> move constructor</td>
 <td>Removed</td>
</tr>

<tr>
 <td>JP 81</td>
 <td></td>
 <td>Conceptification of <code>packaged_task</code></td>
 <td>We don't have concepts now, but <code>uses_allocator</code> trait was added</td>
</tr>

</table>

<h2>Proposed Wording</h2>

<p>As in the rewording nearly every paragraph of [future] has changed, here
is the new complete proposed wording for that clause.
</p>

<h2>Futures [futures]</h2>
<h3>Overview [futures.overview]</h3>
<p>
[futures] describes components that a C++ program can use to retrieve
in one thread the result (value or exception) from a function that has run in
another thread.
</p>
<p>[<em>Note</em>: these components are not restricted to multi-threaded programs
but can be useful in single-threaded programs as well. --<em>end note</em>]
</p>

<h3>Header &lt;future&gt; synopsis</h3>

<pre>
namespace std {
    enum class future_errc
    {
        broken_promise,
        future_already_retrieved,
        future_already_initialized,
        promise_already_satisfied,
        no_state
    };

    template &lt;&gt; struct is_error_code_enum&lt;future_errc&gt; : public true_type {};

    constexpr error_code make_error_code(future_errc e);
    constexpr error_condition make_error_condition(future_errc e);

    extern const error_category* const future_category;

    class future_error;

    template &lt;class R&gt; class promise;
    template &lt;class R&gt; class promise&lt;R&amp;&gt;;
    template &lt;&gt; class promise&lt;void&gt;;

    template &lt;class R&gt; class unique_future;
    template &lt;class R&gt; class unique_future&lt;R&amp;&gt;;
    template &lt;&gt; class unique_future&lt;void&gt;;

    template &lt;class R&gt; class shared_future;
    template &lt;class R&gt; class shared_future&lt;R&amp;&gt;;
    template &lt;&gt; class shared_future&lt;void&gt;;

    template &lt;class R&gt; class atomic_future;
    template &lt;class R&gt; class atomic_future&lt;R&amp;&gt;;
    template &lt;&gt; class atomic_future&lt;void&gt;;

    template &lt;class&gt; class packaged_task;   // <em>undefined</em>
    template &lt;class R, class... ArgTypes&gt;
      class packaged_task&lt;R(ArgTypes...)&gt;;
}
</pre>

<h3>Error handling [futures.errors]</h3>

<pre>
extern const error_category* const future_category;
</pre>
<dl>
<dt></dt>
<dd>
<code>future_category</code> shall point to a statically initialized
object of a type derived from class <code>error_category</code>.
</dd>
<dt></dt>
<dd>
The object's <code>default_error_condition</code> and equivalent
virtual functions shall behave as specified for the class
<code>error_category</code>.
The object's <code>name</code> virtual function shall return a pointer to the string "future".
</dd>
</dl>

<pre>
constexpr error_code make_error_code(future_errc e);
</pre>
<dl>
<dt></dt>
<dd>
<em>Returns:</em>
<code>error_code(static_cast&lt;int&gt;(e), *future_category)</code>.
</dd>
</dl>

<pre>
constexpr error_condition make_error_condition(future_errc e);
</pre>
<dl>
<dt></dt>
<dd>
<em>Returns:</em>
<code>error_condition(static_cast&lt;int&gt;(e), *future_category)</code>.
</dd>
</dl>


<h3>Class <code>future_error</code> [futures.future_error]</h3>
<pre>
  namespace std
  {
    class future_error : public logic_error
    {
    public:
        future_error(error_code ec);

        const error_code&amp; code() const throw();
        const char*       what() const throw();
    };
  }
</pre>

<pre>
future_error(error_code ec);
</pre>
<dl>
<dt></dt>
<dd>
<em>Effects</em>: Constructs an object of class <code>future_error</code>.
<em>Postcondition</em>: <code>ec == code()</code>
</dd>
</dl>

<pre>
const error_code&amp; code() const throw();
</pre>
<dl>
<dt></dt>
<dd>
<em>Returns:</em>
the value of <code>ec</code> that was passed to the object's constructor.
</dd>
</dl>
<pre>
const char *what() const throw();
</pre>
<dl>
<dt></dt>
<dd>
<em>Returns:</em>
an NTBS incorporating <code>code().message()</code>.
</dd>
</dl>

<h3>Associated State</h3>
<p>
If fully initialized,
objects of type <code>unique_future</code> ([future.unique_future]),
<code>shared_future</code> ([future.shared_future]),
<code>atomic_future</code> ([future.atomic_future]),
<code>promise</code> ([future.promise) and
<code>packaged_task</code> ([future.task]) reference
some state that is potentially shared between several such objects.
</p>
<p>
This <em>associated state</em> consists of some
state information and some (possibly not yet evaluated) <em>result</em>, which
can be a (possibly void) value or an exception.
</p>
<p>
The <em>result</em> of such an associated state can be set by</p>
<ul>
 <li><code>promise::set_value</code></li>,
 <li><code>promise::set_exception</code></li> or
 <li><code>packaged_task::operator()</code></li>
</ul>
<p>
When the last reference to an associated state is given up, any resources
held by this associated state are released.
</p>
<p>
An associated state is <em>ready</em> only if it holds a value or an exception
ready for retrieval.
</p>
<p>
The functions that successfully set the stored result of an
associated state synchronize with ([intro.multithread]) calls to
member functions of other objects referring to the same
associated state and such calls are serialized.
The storage of the result (whether normal or
exceptional) into the associated state happens-before
([intro.multithread]) that state is set to <em>ready</em>.
</p>
<p>
Accesses to the same associated state through member functions of
<code>unique_future</code>, <code>shared_future</code>,
<code>atomic_future</code>, <code>promise</code>
or <code>packaged_task</code> objects conflict ([intro.multithread]).
</p>

<h3>Class template <code>promise</code> [futures.promise]</h3>
<pre>
  namespace std {
    template &lt;class R&gt;
    class promise {
    public:
      promise();
      template &lt;class Allocator&gt;
        promise(allocator_arg_t, const Allocator&amp; a);
      promise(promise &amp;&amp; rhs);
      template &lt;class Allocator&gt;
        promise(allocator_arg_t, const Allocator&amp; a,
                promise&amp; rhs);
      promise(const promise&amp; rhs) = delete;
      ~promise();

      // <em>assignment</em>
      promise &amp; operator=(promise&amp;&amp; rhs);
      promise &amp; operator=(const promise&amp; rhs) = delete;
      void swap(promise&amp; other);

      // <em>retrieving the result</em>
      unique_future&lt;R&gt; get_future();

      // <em>setting the result</em>
      void set_value(const R&amp; r);
      void set_value(<em>see below</em>);
      void set_exception(exception_ptr p);
    };

  template &lt;typename R, class Alloc&gt;
    struct uses_allocator&lt;promise&lt;R&gt;, Alloc&gt;;
  }
</pre>

<p>
The implementation shall provide the template <code>promise</code>
and two specializations, <code>promise&lt;R&amp;&gt;</code>
and <code>promise&lt;void&gt;</code>.
These differ only in the argument type of the member function
<code>set_value</code>, as set out in its description, below.
</p>


<pre>
template &lt;typename R, class Alloc&gt;
  struct uses_allocator&lt;promise&lt;R&gt;, Alloc&gt;
   : true_type { };
</pre>

<dl>
<dt></dt>
<dd>
<em>Requires:</em>
<code>Alloc</code> shall be an Allocator ([allocator.requirements] 20.1.2)
</dd>

<dt></dt>
<dd>[<em>Note:</em>
Specialization of this trait informs other library components that
<code>promise</code> can be constructed with an allocator, even
though it does not have a nested <code>allocator_type</code> associated type.
--<em>end note</em>]
</dd>
</dl>


<pre>
promise();
template &lt;class Allocator&gt;
  promise(allocator_arg_t, const Allocator&amp; a);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
constructs a <code>promise</code> object and an associated state.
The second constructor uses the allocator <code>a</code>
to allocate memory for the associated state.
</dd>
</dl>


<pre>
promise(promise &amp;&amp; rhs);
template &lt;class Allocator&gt;
  promise(allocator_arg_t, const Allocator&amp; a, promise&amp; rhs);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
Constructs a new <code>promise</code> object and
transfers ownership of the associated state of <code>rhs</code> (if
any) to the newly-constructed object.
</dd>

<dt></dt>
<dd>
<em>Postcondition:</em>
<code>rhs</code> has no associated state.
</dd>
</dl>


<pre>
~promise();
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
If the state associated with <code>*this</code> is
not <em>ready</em>, stores an exception of
type <code>future_error</code> with an error code
of <code>broken_promise</code> as-if
by <code>this-&gt;set_exception(copy_exception(future_error(future_errc::broken_promise)))</code>.
</dd>
<dt></dt>
<dd>
Destroys <code>*this</code> and releases its reference to its
associated state if any. If this is the last reference to that
associated state, destroys that state.
</dd>
</dl>


<pre>
promise&amp; operator=(promise&amp;&amp; rhs);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
As-if:
<pre>std::promise&lt;R&gt;(std::move(rhs)).swap(*this);</pre>
</dd>

<dt></dt>
<dd>
<em>Postcondition:</em>
<code>rhs</code> has no associated state. <code>*this</code> has
the associated state of <code>rhs</code> prior to the assignment.
</dd>

<dt></dt>
<dd>
<em>Returns:</em>
<code>*this</code>.
</dd>

<dt></dt>
<dd>
<em>Throws:</em>
nothing.
</dd>
</dl>


<pre>
void swap(promise&amp; other);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
Exchanges the associated states of <code>*this</code> and <code>other</code>.
</dd>
</dl>

<p><em>Postcondition:</em>
<code>*this</code> has the same associated state (if any)
as <code>other</code> prior to the call
to <code>swap</code>. <code>other</code> has the same associated
state (if any) as <code>*this</code> prior to the call
to <code>swap</code>.
</p>


<pre>
unique_future&lt;R&gt; get_future();
</pre>

<dl>
<dt></dt>
<dd>
<em>Returns:</em>
a <code>unique_future&lt;R&gt;</code> object with the same associated state
as <code>*this</code>.
</dd>

<dt></dt>
<dd>
<em>Throws:</em>
<code>future_error</code> if <code>*this</code> has no associated
state, or if <code>get_future()</code> has already been called on
a <code>promise</code> with the associated state
of <code>*this</code>.
</dd>
<dt></dt>
<dd>
<em>Error conditions:</em>
<ul>
  <li>
<code>future_already_retrieved</code> if <code>get_future</code>
    has already been called on
    a <code>promise</code> with the associated state
    of <code>*this</code>.</li>
  <li>
<code>no_state</code> if <code>*this</code> has no associated
    state.</li>
</ul>
</dd>
</dl>


<pre>
void set_value(const R&amp; r);
void promise::set_value(R&amp;&amp; r);
void promise&lt;R&amp;&gt;::set_value(R&amp; r);
void promise&lt;void&gt;::set_value();
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
Atomically stores <code>r</code> in the associated state and sets that
state to ready. Any threads blocked in a call of a blocking function
of any future that refers to the same associated
state as <code>*this</code> are unblocked.
</dd>

<dt></dt>
<dd>
<em>Throws:</em>
<code>future_error</code> if its associated state is already
ready.
</dd>

<dt></dt>
<dd>
<em>Error conditions:</em>
<ul>
  <li>
<code>promise_already_satisfied</code> if its associated state is already
    ready.</li>
  <li>
<code>no_state</code> if <code>*this</code> has no associated
  state.</li>
</ul>
</dd>

<dt></dt>
<dd>
<em>Synchronization:</em>
Calls to <code>set_value</code> and <code>set_exception</code> on
a single <code>promise</code> object are serialized.
</dd>
<dt></dt>
<dd>[<em>Note</em>:
And they synchronize and serialize with other functions through the
referred associated state. --<em>end note</em>]
</dd>
</dl>



<pre>
void set_exception(exception_ptr p);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
Atomically stores <code>p</code> in the associated state and sets that
state to ready. Any threads blocked in a call of a blocking function
of any future that refers to the same associated
state as <code>*this</code> are unblocked.
</dd>

<dt></dt>
<dd>
<em>Throws:</em>
<code>future_error</code> if its associated state is already
ready.
</dd>

<dt></dt>
<dd>
<em>Error conditions:</em>
<ul>
  <li>
<code>promise_already_satisfied</code> if its associated state is already
    ready.</li>
  <li>
<code>no_state</code> if <code>*this</code> has no associated
  state.</li>
</ul>
</dd>

<dt></dt>
<dd>
<em>Synchronization:</em>
Calls to <code>set_value</code> and <code>set_exception</code> on
a single <code>promise</code> object are serialized.
</dd>
<dt></dt>
<dd>[<em>Note</em>:
And they synchronize and serialize with other functions through the
referred associated state. --<em>end note</em>]
</dd>
</dl>


<h3>Class template <code>unique_future</code> [futures.unique_future]</h3>
<p>
The class template <code>unique_future</code> defines a type for
asynchronous return objects which do not share their associated
state. A default-constructed <code>unique_future</code> has no
associated state. <code>unique_future</code> objects with associated
state can only be created by use of
<code>promise</code> ([future.promise]) or <code>packaged_task</code>
([future.task]) objects, and share their associated state with
that <code>promise</code> or <code>packaged_task</code>.  Their
values or exceptions can be set by use of the <code>promise</code>
([future.promise]) or <code>packaged_task</code> ([future.task])
object that shares the same associated state.
</p>
<p>[<em>Note</em>:
<code>unique_future</code> member functions do not synchronize with
themselves or with member functions of <code>shared_future</code>.
--<em>end note</em>]
</p>
<p>
The effect of calling any member function other than the destructor
or the move-assignment operator on a <code>unique_future</code>
for which <code>valid() == false</code> is undefined.
</p>

<pre>
  namespace std {
    template &lt;typename R&gt;
    class unique_future {
    public:
      unique_future();
      unique_future(unique_future &amp;&amp;);
      unique_future(const unique_future &amp; rhs) = delete;
      ~unique_future();
      unique_future&amp; operator=(const unique_future &amp; rhs) = delete;
      unique_future&amp; operator=(unique_future&amp;&amp; rhs);

      // <em>retrieving the value</em>
      <em>see below</em> get();

      // <em>functions to check state and wait for ready</em>
      bool is_ready() const;
      bool has_exception() const;
      bool has_value() const;
      bool valid() const;

      void wait() const;
      template &lt;class Rep, class Period&gt;
        bool wait_for(const chrono::duration&lt;Rep, Period&gt;&amp; rel_time) const;
      template &lt;class Clock, class Duration&gt;
        bool wait_until(const chrono::time_point&lt;Clock, Duration&gt;&amp; abs_time) const;
    };
  }
</pre>

<p>
The implementation shall provide the template <code>unique_future</code>
and two specializations, <code>unique_future&lt;R&amp;&gt;</code>
and <code>unique_future&lt;void&gt;</code>.
These differ only in the return type and return value of the member function
<code>get</code>, as set out in its description, below.
</p>


<pre>
unique_future();
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
constructs an <em>empty</em> <code>unique_future</code> that doesn't
refer to an associated state.
</dd>
<dt></dt>
<dd>
<em>Postcondition:</em>
<code>valid() == false</code>
</dd>
</dl>


<pre>
unique_future(unique_future&amp;&amp; rhs);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
move constructs a <code>unique_future</code> object that refers to the
associated state that was originally referred to by <code>rhs</code> (if any).
</dd>
<dt></dt>
<dd>
<em>Postcondition:</em>
<ul>
  <li>
<code>valid</code> returns the same value as <code>rhs.valid()</code>
    prior to the constructor invocation.</li>
  <li>
<code>rhs.valid() == false</code>.</li>
</ul>
</dd>
</dl>


<pre>
~unique_future();
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
<ul>
  <li>gives up the reference to its associated state.</li>
  <li>destroys <code>*this</code>.</li>
</ul>
</dd>
</dl>


<pre>
unique_future&amp; operator=(unique_future&amp;&amp; rhs);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
<ul>
  <li>if <code>*this</code> referred to an associated state prior to
      the assignment it gives up this reference.</li>
  <li>assigns the contents of <code>rhs</code> to<code>*this</code>.</li>
</ul>
</dd>
<dt></dt>
<dd>
<em>Postcondition:</em>
<ul>
  <li>
<code>valid</code> returns the same value as <code>rhs.valid()</code>
    prior to the constructor invocation.</li>
  <li>
<code>rhs.valid() == false</code>.</li>
</ul>
</dd>
</dl>


<pre>
R&amp;&amp; get();
R&amp; unique_future&lt;R&amp;&gt;::get(); // this is for the specialization unique_future&lt;R&amp;&gt;
void unique_future&lt;void&gt;::get(); // this is for the specialization unique_future&lt;void&gt;
</pre>

<dl>
<dt></dt>
<dd>
<em>Note:</em>
as described above, the template and its two required specializations
differ only in the return type and return value of the member function
<code>get</code>.
</dd>

<dt></dt>
<dd>
<em>Precondition:</em>
<code>valid() == true</code>
</dd>

<dt></dt>
<dd>
<em>Effects:</em>
<ul>
 <li>
<code>wait()</code>s until <code>is_ready()</code>.</li>
 <li>Retrieves the value stored in the associated state.</li>
</ul>
</dd>
<dt></dt>
<dd>
<em>Returns:</em>
<ul>
 <li>
<code>unique_future::get()</code> returns an rvalue-reference to the
     value stored in the object's associated state.</li>
 <li>
<code>unique_future&lt;R&amp;&gt;::get()</code> returns the reference
      stored as value in the object's associated state.</li>
 <li>
<code>unique_future&lt;void&gt;::get()</code> returns nothing.</li>
</ul>
</dd>

<dt></dt>
<dd>
<em>Throws:</em>
the stored exception, if an exception was stored in the associated state.
</dd>

<dt></dt>
<dd>
<em>Postcondition:</em>:
<code>valid() == false</code>
</dd>
</dl>

<pre>
bool is_ready() const;
</pre>

<p><em>Precondition:</em>
<code>valid() == true</code>
</p>

<dl>
<dt></dt>
<dd>
<em>Returns:</em>
<code>true</code> only if the associated state holds a value or an exception
ready for retrieval.
</dd>
<dt></dt>
<dd>
<em>Sychronization:</em>
If <code>is_ready()</code> returns <code>true</code>, the setting of the
object's associated state happens before ([intro.multithread])
<code>is_ready()</code> returns.
</dd>
<dt></dt>
<dd>
This synchronization specification also holds for all other member
functions whose semantics depend on <code>is_ready() == true</code>.
</dd>
</dl>

<pre>
bool has_exception() const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Precondition:</em>
<code>valid() == true</code>
</dd>

<dt></dt>
<dd>
<em>Returns:</em>
<code>true</code> only if <code>is_ready()</code> returns <code>true</code>
and the associated state contains an exception.
</dd>
</dl>

<pre>
bool has_value() const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Precondition:</em>
<code>valid() == true</code>
</dd>

<dt></dt>
<dd>
<em>Returns:</em>
<code>true</code> only if <code>is_ready()</code> returns <code>true</code>
and the associated state contains a value.
</dd>
</dl>


<pre>
bool valid() const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Returns:</em>
<code>true</code> only if <code>*this</code> refers to an associated state.
</dd>
</dl>


<pre>
void wait() const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Precondition:</em>
<code>valid() == true</code>
</dd>

<dt></dt>
<dd>
<em>Effects:</em>
blocks until <code>is_ready()</code> returns <code>true</code>.
</dd>
</dl>

<pre>
template &lt;class Rep, class Period&gt;
  bool wait_for(const chrono::duration&lt;Rep, Period&gt;&amp; rel_time) const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Precondition:</em>
<code>valid() == true</code>
</dd>

<dt></dt>
<dd>
<em>Effects:</em>
blocks until <code>is_ready()</code> returns <code>true</code>
or until <code>rel_time</code> has elapsed.
</dd>
<dt></dt>
<dd>
<em>Returns:</em>
<code>is_ready()</code>.
</dd>
</dl>

<pre>
template &lt;class Clock, class Duration&gt;
  bool wait_until(const chrono::time_point&lt;Clock, Duration&gt;&amp; abs_time) const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Precondition:</em>
<code>valid() == true</code>
</dd>

<dt></dt>
<dd>
<em>Effects:</em>
blocks until <code>is_ready()</code> returns <code>true</code>
or until the current time exceeds <code>abs_time</code>.
</dd>
<dt></dt>
<dd>
<em>Returns:</em>
<code>is_ready()</code>.
</dd>
</dl>



<h3>Class template <code>shared_future</code> [future.shared_future]</h3>
<p>
The class template <code>shared_future</code> defines a type for
asynchronous return objects which may share their associated state. A
default-constructed <code>shared_future</code> has no associated
state. A <code>shared_future</code> object with associated state can
only be created from another <code>shared_future</code> with
associated state or a <code>unique_future</code> object with
associated state.  Their values or exceptions can be set by use of
a <code>promise</code> ([future.promise])
or <code>packaged_task</code> ([future.task]) object that shares the
same associated state.
</p>
<p>[<em>Note:</em><code>shared_future</code> member functions do not
synchronize with themselves, but they synchronize on the shared
associated state. --end note]
</p>

<pre>
  namespace std {
    template &lt;class R&gt;
    class shared_future {
    public:
      shared_future();
      shared_future(const shared_future&amp; rhs);
      shared_future(unique_future&lt;R&gt;&amp;&amp;);
      shared_future(shared_future&amp;&amp;);
      ~shared_future();
      shared_future &amp; operator=(const shared_future&amp; rhs);
      shared_future &amp; operator=(shared_future&amp;&amp; rhs);

      // <em>retrieving the value</em>
      <em>see below</em> get() const;

      // <em>functions to check state and wait for ready</em>
      bool is_ready() const;
      bool has_exception() const;
      bool has_value() const;
      bool valid() const;

      void wait() const;
      template &lt;class Rep, class Period&gt;
        bool wait_for(const chrono::duration&lt;Rep, Period&gt;&amp; rel_time) const;
      template &lt;class Clock, class Duration&gt;
        bool wait_until(const chrono::time_point&lt;Clock, Duration&gt;&amp; abs_time) const;
    };
  }
</pre>


<p>
The implementation shall provide the template <code>shared_future</code>
and two specializations, <code>shared_future&lt;R&amp;&gt;</code>
and <code>shared_future&lt;void&gt;</code>.
These differ only in the return type and return value of the member function
<code>get</code>, as set out in its description, below.
</p>


<pre>
shared_future();
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
constructs an <em>empty</em> <code>shared_future</code> that doesn't
refer to an associated state.
</dd>
<dt></dt>
<dd>
<em>Postcondition:</em>
<code>valid() == false</code>
</dd>
</dl>


<pre>
shared_future(const shared_future&amp; rhs);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
constructs a <code>shared_future</code> object that refers to the same
associated state as <code>rhs</code> (if any).
</dd>
<dt></dt>
<dd>
<em>Postcondition:</em>
<code>valid</code> returns the same value as <code>rhs.valid()</code>
</dd>
</dl>


<pre>
shared_future(unique_future&lt;R&gt;&amp;&amp; rhs);
shared_future(shared_future&amp;&amp; rhs);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
move constructs a <code>shared_future</code> object that refers to the
associated state that was originally referred to by <code>rhs</code> (if any).
</dd>
<dt></dt>
<dd>
<em>Postcondition:</em>
<ul>
  <li>
<code>valid</code> returns the same value as <code>rhs.valid()</code>
    prior to the constructor invocation.</li>
  <li>
<code>rhs.valid() == false</code>.</li>
</ul>
</dd>
</dl>

<pre>
~shared_future();
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
<ul>
  <li>gives up the reference to its associated state.</li>
  <li>destroys <code>*this</code>.</li>
</ul>
</dd>
</dl>


<pre>
shared_future&amp; operator=(shared_future&amp;&amp; rhs);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
<ul>
  <li>if <code>*this</code> referred to an associated state prior to
      the assignment it gives up this reference.</li>
  <li>assigns the contents of <code>rhs</code> to<code>*this</code>.</li>
</ul>
</dd>
<dt></dt>
<dd>
<em>Postcondition:</em>
<ul>
  <li>
<code>valid</code> returns the same value as <code>rhs.valid()</code>
    prior to the constructor invocation.</li>
  <li>
<code>rhs.valid() == false</code>.</li>
</ul>
</dd>
</dl>


<pre>
shared_future&amp; operator=(shared_future&amp;&amp; rhs);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
<ul>
  <li>if <code>*this</code> referred to an associated state prior to
      the assignment it gives up this reference.</li>
  <li>assigns the contents of <code>rhs</code> to<code>*this</code>.
    [<em>Note</em>: So <code>*this</code> refers to the same associated
    state as <code>rhs</code> (if any). --<em>end note</em>]</li>
</ul>
</dd>
<dt></dt>
<dd>
<em>Postcondition:</em>
<code>valid</code> returns the same value as <code>rhs.valid()</code>
</dd>
</dl>


<pre>
const R&amp; shared_future::get() const;
R&amp; shared_future&lt;R&amp;&gt;::get() const;
void shared_future&lt;void&gt;::get() const;
</pre>

<dl>
<dt></dt>
<dd>
<em>Note:</em>
as described above, the template and its two required specializations
differ only in the return type and return value of the member function
<code>get</code>.
</dd>

<dt></dt>
<dd>
<em>Precondition:</em>
<code>valid() == true</code>
</dd>

<dt></dt>
<dd>
<em>Effects:</em>
<ul>
 <li>
<code>wait()</code>s until <code>is_ready()</code>.</li>
 <li>Retrieves the value stored in the associated state.</li>
</ul>
</dd>
<dt></dt>
<dd>
<em>Returns:</em>
<ul>
 <li>
<code>shared_future::get()</code> returns a const reference to the
     value stored in the object's associated state.</li>
 <li>
<code>shared_future&lt;R&amp;&gt;::get()</code> returns the reference
      stored as value in the object's associated state.</li>
 <li>
<code>unique_future&lt;void&gt;::get()</code> returns nothing.</li>
</ul>
</dd>

<dt></dt>
<dd>
<em>Throws:</em>
the stored exception, if an exception was stored in the associated state.
</dd>
</dl>


<pre>
bool is_ready() const;
</pre>

<dl>
<dt></dt>
<dd>
<em>Returns:</em>
<code>true</code> only if <code>valid()</code> returns
<code>true</code> and the associated state holds a value or an exception
ready for retrieval.
</dd>
<dt></dt>
<dd>
<em>Sychronization:</em>
If <code>is_ready()</code> returns <code>true</code>, the setting of the
object's associated state happens before ([intro.multithread])
<code>is_ready()</code> returns.
</dd>
<dt></dt>
<dd>
This synchronization specification also holds for all other member
functions whose semantics depend on <code>is_ready() == true</code>.
</dd>
</dl>


<pre>
bool has_exception() const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Returns:</em>
<code>true</code> only if <code>is_ready()</code> returns <code>true</code>
and the associated state contains an exception.
</dd>
</dl>

<pre>
bool has_value() const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Returns:</em>
<code>true</code> only if <code>is_ready()</code> returns <code>true</code>
and the associated state contains a value.
</dd>
</dl>


<pre>
bool valid() const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Returns:</em>
<code>true</code> only if <code>*this</code> refers to an associated state.
</dd>
</dl>


<pre>
void wait() const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Effects:</em>
blocks until <code>is_ready()</code> returns <code>true</code>.
</dd>
</dl>


<pre>
template &lt;class Rep, class Period&gt;
  bool wait_for(const chrono::duration&lt;Rep, Period&gt;&amp; rel_time) const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Effects:</em>
blocks until <code>is_ready()</code> returns <code>true</code>
or until <code>rel_time</code> has elapsed.
</dd>
<dt></dt>
<dd>
<em>Returns:</em>
<code>is_ready()</code>.
</dd>
</dl>

<pre>
template &lt;class Clock, class Duration&gt;
  bool wait_until(const chrono::time_point&lt;Clock, Duration&gt;&amp; abs_time) const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Effects:</em>
blocks until <code>is_ready()</code> returns <code>true</code>
or until the current time exceeds <code>abs_time</code>.
</dd>
<dt></dt>
<dd>
<em>Returns:</em>
<code>is_ready()</code>.
</dd>
</dl>



<h3>Class template <code>atomic_future</code> [future.atomic_future]</h3>
<p>
 The class template <code>atomic_future</code> defines a type for
 asynchronous return objects.
 These objects can only be created by use of
 <code>promise</code> ([future.promise])
 or <code>packaged_task</code> ([future.task]) objects.
 Their values or exceptions can be set
 by use of <code>promise</code> ([future.promise]) objects.
</p>
<p>
 Contrary to <code>unique_future</code> and <code>shared_future</code>,
 calls to member functions of <code>atomic_future</code> synchronize
 with ([intro.multithread]) one another.
</p>

<pre>
  namespace std {
    template &lt;class R&gt;
    class atomic_future {
    public:
      atomic_future();
      atomic_future(const atomic_future&amp; rhs);
      atomic_future(unique_future&lt;R&gt;&amp;&amp; rhs);
      ~atomic_future();
      atomic_future &amp; operator=(const atomic_future&amp; rhs);

      // <em>retrieving the value</em>
      <em>see below</em> get() const;

      // <em>functions to check state and wait for ready</em>
      bool is_ready() const;
      bool has_exception() const;
      bool has_value() const;
      bool valid() const;

      void wait() const;
      template &lt;class Rep, class Period&gt;
        bool wait_for(const chrono::duration&lt;Rep, Period&gt;&amp; rel_time) const;
      template &lt;class Clock, class Duration&gt;
        bool wait_until(const chrono::time_point&lt;Clock, Duration&gt;&amp; abs_time) const;
    };
  }
</pre>


<p>
The implementation shall provide the template <code>atomic_future</code>
and two specializations, <code>atomic_future&lt;R&amp;&gt;</code>
and <code>atomic_future&lt;void&gt;</code>.
These differ only in the return type and return value of the member functions
<code>get</code> and <code>try_get</code>,
as set out in its description, below.
</p>


<pre>
atomic_future();
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
constructs an <em>empty</em> <code>atomic_future</code> that doesn't
refer to an associated state.
</dd>
<dt></dt>
<dd>
<em>Postcondition:</em>
<code>valid() == false</code>
</dd>
</dl>


<pre>
atomic_future(const atomic_future&amp; rhs);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
constructs an <code>atomic_future</code> object that refers to the same
associated state as <code>rhs</code> (if any).
</dd>
<dt></dt>
<dd>
<em>Postcondition:</em>
<code>valid</code> returns the same value as <code>rhs.valid()</code>
</dd>
</dl>


<pre>
atomic_future(const unique_future&lt;R&gt;&amp;&amp; rhs);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
move constructs an <code>atomic_future</code> object  that refers to the
associated state that was originally referred to by <code>rhs</code> (if any).
</dd>
<dt></dt>
<dd>
<em>Postcondition:</em>
<ul>
  <li>
<code>valid</code> returns the same value as <code>rhs.valid()</code>
    prior to the constructor invocation.</li>
  <li>
<code>rhs.valid() == false</code>.</li>
</ul>
</dd>
</dl>

<pre>
~atomic_future();
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
<ul>
  <li>gives up the reference to its associated state.</li>
  <li>destroys <code>*this</code>.</li>
</ul>
</dd>
</dl>


<pre>
atomic_future&amp; operator=(const atomic_future &amp; rhs);
</pre>

<dl>
<dt></dt>
<dd>
<em>Precondition:</em>
<code>this-&gt;valid() == false</code>
</dd>
<dt></dt>
<dd>
<em>Effects:</em>
assigns the contents of <code>rhs</code> to <code>*this</code>.
[<em>Note</em>: So <code>*this</code> refers to the same associated
state as <code>rhs</code> (if any). --<em>end note</em>]
</dd>
<dt></dt>
<dd>
<em>Postcondition:</em>
<code>valid</code> returns the same value as <code>rhs.valid()</code>
</dd>
<dt></dt>
<dd>
<em>Throws:</em>
<code>future_error</code> with an error condition of
<code>future_already_initialized</code> if the precondition isn't met.
</dd>
</dl>


<pre>
const R&amp; atomic_future::get() const;
R&amp; atomic_future&lt;R&amp;&gt;::get() const;
void atomic_future&lt;void&gt;::get() const;
</pre>

<dl>
<dt></dt>
<dd>
<em>Note:</em>
as described above, the template and its two required specializations
differ only in the return type and return value of the member function
<code>get</code>.
</dd>

<dt></dt>
<dd>
<em>Precondition:</em>
<code>valid() == true</code>
</dd>

<dt></dt>
<dd>
<em>Effects:</em>
<ul>
 <li>
<code>wait()</code>s until <code>is_ready()</code>.</li>
 <li>Retrieves the value stored in the associated state.</li>
</ul>
</dd>
<dt></dt>
<dd>
<em>Returns:</em>
<ul>
 <li>
<code>atomic_future::get()</code> returns an const reference to the
     value stored in the object's associated state.</li>
 <li>
<code>atomic_future&lt;R&amp;&gt;::get()</code> returns the reference
      stored as value in the object's associated state.</li>
 <li>
<code>atomic_future&lt;void&gt;::get()</code> returns nothing.</li>
</ul>
</dd>
<dt></dt>
<dd>
<em>Throws:</em>
the exception in the object's associated state, if an exception
was stored there or <code>future_error</code> with an error condition of
<code>no_state</code> if the precondition isn't met.
</dd>
<dt></dt>
<dd>[<em>Note:</em>:
Contrary to <code>unique_future</code>, calling <code>get()</code>
more than once on the same object is well defined and just produces
the result again. --<em>end note</em>]
</dd>
</dl>


<pre>
bool is_ready() const;
</pre>

<dl>
<dt></dt>
<dd>
<em>Returns:</em>
<code>true</code> only if <code>valid()</code> returns
<code>true</code> and the associated state holds a value or an exception
ready for retrieval.
</dd>
<dt></dt>
<dd>
<em>Sychronization:</em>
If <code>is_ready()</code> returns <code>true</code>, the setting of the
object's associated state happens before ([intro.multithread])
<code>is_ready()</code> returns.
</dd>
<dt></dt>
<dd>
This synchronization specification also holds for all other member
functions whose semantics depend on <code>is_ready() == true</code>.
</dd>
</dl>


<pre>
bool has_exception() const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Returns:</em>
<code>true</code> only if <code>is_ready()</code> returns <code>true</code>
and the associated state contains an exception.
</dd>
</dl>

<pre>
bool has_value() const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Returns:</em>
<code>true</code> only if <code>is_ready()</code> returns <code>true</code>
and the associated state contains a value.
</dd>
</dl>


<pre>
bool valid() const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Returns:</em>
<code>true</code> only if <code>*this</code> refers to an associated state.
</dd>
</dl>


<pre>
void wait() const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Effects:</em>
blocks until <code>is_ready()</code> returns <code>true</code>.
</dd>
</dl>


<pre>
template &lt;class Rep, class Period&gt;
  bool wait_for(const chrono::duration&lt;Rep, Period&gt;&amp; rel_time) const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Effects:</em>
blocks until <code>is_ready()</code> returns <code>true</code>
or until <code>rel_time</code> has elapsed.
</dd>
<dt></dt>
<dd>
<em>Returns:</em>
<code>is_ready()</code>.
</dd>
</dl>

<pre>
template &lt;class Clock, class Duration&gt;
  bool wait_until(const chrono::time_point&lt;Clock, Duration&gt;&amp; abs_time) const;
</pre>
<dl>
<dt></dt>
<dd>
<em>Effects:</em>
blocks until <code>is_ready()</code> returns <code>true</code>
or until the current time exceeds <code>abs_time</code>.
</dd>
<dt></dt>
<dd>
<em>Returns:</em>
<code>is_ready()</code>.
</dd>
</dl>



<h3>Class template <code>packaged_task</code> [futures.task]</h3>
<p>
A <code>packaged_task</code> provides a means of wrapping a
function or callable object so that the return value of the function
or callable object is stored in a future when it is invoked. The
associated state of a <code>packaged_task</code> object includes
storage for a copy of this associated task.
</p>
<p>
When the <code>packaged_task</code> is invoked, its associated task
is invoked, and the result (whether normal or exceptional) stored in
the associated state. Any futures that share the associated state
will then be able to access the stored result.
</p>

<pre>
  namespace std {
    template&lt;class&gt; class packaged_task; // <em>undefined</em>
    template&lt;class R, class... ArgTypes&gt;
    class packaged_task&lt;R(ArgTypes...)&gt; {
    public:
      typedef R result_type;

      // construction and destruction
      packaged_task();
      template &lt;class F&gt;
        explicit packaged_task(F f);
      template &lt;class F, class Allocator&gt;
        explicit packaged_task(allocator_arg_t, const Allocator&amp; a, F f);
      explicit packaged_task(R(*f)(ArgTypes...));
      template &lt;class F&gt;
        explicit packaged_task(F&amp;&amp; f);
      template &lt;class F, class Allocator&gt;
        explicit packaged_task(allocator_arg_t, const Allocator&amp; a, F&amp;&amp; f);
        ~packaged_task();

        // no copy
        packaged_task(packaged_task&amp;) = delete;
        packaged_task&amp; operator=(packaged_task&amp;) = delete;

        // move support
        packaged_task(packaged_task&amp;&amp; other);
        packaged_task&amp; operator=(packaged_task&amp;&amp; other);
        void swap(packaged_task&amp; other);
        explicit operator bool() const;

        // result retrieval
        unique_future&lt;R&gt; get_future();

        // execution
        void operator()(ArgTypes... );
        void reset();
    };
}
</pre>

<pre>packaged_task();</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
constructs a <code>packaged_task</code> object with no
associated state or task.
</dd>
<dt></dt>
<dd>
<em>Throws:</em>nothing.
</dd>
</dl>

<pre>
template &lt;class F&gt;
  packaged_task(F f);
template &lt;class F, class Allocator&gt;
  explicit packaged_task(allocator_arg_t, const Allocator&amp; a, F f);
packaged_task(R(*f)(ArgTypes...));
template &lt;class F&gt;
  packaged_task(F&amp;&amp; f);
template &lt;class F, class Allocator&gt;
  explicit packaged_task(allocator_arg_t, const Allocator&amp; a, F&amp;&amp; f);
</pre>

<dl>
<dt></dt>
<dd>
<em>Precondition:</em>
<code>INVOKE (f, t1, t2, ..., tN, R)</code>
where <code>t1</code>, <code>t2</code>, ..., <code>tN</code> are
values of the corresponding types in <code>ArgTypes....</code>
shall be a valid expression. Invoking a copy of <code>f</code>
shall behave the same as invoking <code>f</code>.
</dd>

<dt></dt>
<dd>
<em>Effects:</em>
constructs a new <code>packaged_task</code> object with a copy
of <code>f</code> stored in the associated state as the object's
associated task.  The constructors that take an <code>Allocator</code>
argument use it to allocate memory needed to store the internal data
structures.
</dd>

<dt></dt>
<dd>
<em>Throws:</em>
any exceptions thrown by the copy or move constructor of <code>f</code>,
or <code>std::bad_alloc</code> if memory
for the associated state could not be allocated.
</dd>
</dl>


<pre>
packaged_task(packaged_task&amp;&amp; other);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
constructs a new <code>packaged_task</code>
object and transfers ownership of <code>other</code>'s associated
state to <code>*this</code>,
leaving <code>other</code> with no associated state.
</dd>
<dt></dt>
<dd>
<em>Postcondition:</em>
<code>other</code> has no associated state.
</dd>
</dl>

<pre>
packaged_task&amp; operator=(packaged_task&amp;&amp; other);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
As-if:
<pre>
std::packaged_task&lt;R,ArgTypes...&gt;(other).swap(*this);
</pre>
</dd>
</dl>

<pre>
~packaged_task();
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
If the state associated with <code>*this</code> is
not <em>ready</em>, stores an exception of
type <code>future_error</code> with an error code
of <code>broken_promise</code>. Any threads blocked in a
member function
of <code>unique_future</code>, <code>shared_future</code>
or <code>atomic_future</code> waiting for the state associated
with <code>*this</code> to become <em>ready</em> are unblocked.
</dd>
<dt></dt>
<dd>
Destroys <code>*this</code> and releases its reference to its
associated state if any. If this is the last reference to that
associated state, destroys that state.
</dd>
<dt></dt>
<dd>
<em>Throws:</em>
nothing.
</dd>
</dl>

<pre>
void swap(packaged_task&amp; other);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
exchanges the associated states of <code>*this</code> and <code>other</code>.
</dd>
<dt></dt>
<dd>
<em>Postcondition:</em>
<code>*this</code> has the same associated state (if any)
as <code>other</code> prior to the call
to <code>swap</code>. <code>other</code> has the same associated
state (if any) as <code>*this</code> prior to the call
to <code>swap</code>.
</dd>
<dt></dt>
<dd>
<em>Throws:</em>
nothing.
</dd>
</dl>

<pre>
explicit operator bool() const;
</pre>

<dl>
<dt></dt>
<dd>
<em>Returns:</em>
<code>true</code> if and only if <code>*this</code> has an associated state.
</dd>
<dt></dt>
<dd>
<em>Throws:</em>
nothing.
</dd>
</dl>

<pre>
unique_future&lt;R&gt; get_future();
</pre>

<dl>
<dt></dt>
<dd>
<em>Returns:</em>
a <code>unique_future</code> object that
shares the same associated state as <code>*this</code>.
</dd>
<dt></dt>
<dd>
<em>Throws:</em>
<code>std::future_error</code> if an error occurs.
</dd>
<dt></dt>
<dd>
<em>Error conditions:</em>
<ul>
  <li>
<code>future_already_retrieved</code> if <code>get_future</code>
    has already been called on
    a <code>packaged_task</code> with the associated state
    of <code>*this</code>.</li>
  <li>
<code>no_state</code> if <code>*this</code> has no associated
    state.</li>
</ul>
</dd>
</dl>

<pre>
void operator()(ArgTypes... args);
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
<code>INVOKE (f, t1, t2, ..., tN, R)</code>,
where <code>f</code> is the task in associated state
of <code>*this</code> and <code>t1</code>, <code>t2</code>,
..., <code>tN</code> are the values in <code>args...</code>. If
the task returns normally, the return value is stored as the
asynchronous result in the state associated
with <code>*this</code>, otherwise the exception thrown by the
task is stored. The state associated with <code>*this</code> is
made <em>ready</em>, and any threads blocked in a member function
of <code>unique_future</code>, <code>shared_future</code>
or <code>atomic_future</code> waiting for the state associated
with <code>*this</code> to become <em>ready</em> are
unblocked.
</dd>
<dt></dt>
<dd>
<em>Throws:</em>
<code>std::future_error</code> if there is no
  associated state or the task has already been invoked.
</dd>
<dt></dt>
<dd>
<em>Error conditions:</em>
<ul>
  <li>
<code>no_state</code> if <code>*this</code> has no associated
    state.</li>
  <li>
<code>promise_already_satisfied</code> if the associated
    state is already <em>ready</em>.</li>
</ul>
</dd>

<dt></dt>
<dd>
<em>Synchronization:</em>
&gt;A successful call to <code>operator()</code> synchronizes with
([intro.multithread]) a call to any member function of
a <code>unique_future</code> object, <code>shared_future</code>
object or <code>atomic_future</code> object that shares the same
associated state. The completion of the invocation of the
associated task and the storage of the result (whether normal or
exceptional) into the associated state happens-before
([intro.multithread]) that state is set to <em>ready</em>.
</dd>
<dt></dt>
<dd>[<em>Note</em>:
<code>operator()</code> synchronizes and serializes with other functions
through the referred associated state. --<em>end note</em>]
</dd>
</dl>

<pre>
void reset();
</pre>

<dl>
<dt></dt>
<dd>
<em>Effects:</em>
returns the object to a state as if a
newly-constructed instance had just been assigned to
<code>*this</code> by <code>*this = packaged_task(std::move(f))</code>,
where <code>f</code> is the task
stored in the associated state of <code>*this</code>.
</dd>
<dt></dt>
<dd>
[<em>Note:</em> This constructs a new associated state
for <code>*this</code>. The old state is discarded, as described in
the destructor
for <code>packaged_task</code>. <code>get_future()</code> may now be
called again for <code>*this</code>. -- <em>End Note</em>]
</dd>

<dt></dt>
<dd>
<em>Throws:</em>
<ul>
  <li>
<code>std::bad_alloc</code> if memory for the new
    associated state could not be allocated.</li>
  <li>Any exception thrown by the copy constructor of the task
    stored in the associated state.</li>
  <li>
<code>std::future_error</code> with an error condition
    of <code>no_state</code> if <code>*this</code> has no
    associated state</li>
</ul>
</dd>
</dl>

  </body>
</html>
