<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1266: shared_future::get and deferred async functions</title>
<meta property="og:title" content="Issue 1266: shared_future::get and deferred async functions">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1266.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#Resolved">Resolved</a> status.</em></p>
<h3 id="1266"><a href="lwg-defects.html#1266">1266</a>. <code>shared_future::get</code> and deferred <code>async</code> functions</h3>
<p><b>Section:</b> 32.10.8 <a href="https://wg21.link/futures.shared.future">[futures.shared.future]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Anthony Williams <b>Opened:</b> 2009-11-17 <b>Last modified:</b> 2021-06-06</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#futures.shared.future">issues</a> in [futures.shared.future].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
If a <code>shared_future</code> is constructed with the result of an <code>async</code> call with a
deferred function, and two or more copies of that <code>shared_future</code> are created,
with multiple threads calling <code>get()</code>, it is not clear which thread runs the
deferred function.  [futures.shared_future]p22 from
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n3000.pdf">N3000</a>
says (minus editor's note):
</p>

<blockquote><p>
<i>Effects:</i> if the associated state contains a deferred function, executes
the deferred function. Otherwise, blocks until the associated state is ready.
</p></blockquote>

<p>
In the absence of wording to the contrary, this implies that every thread that
calls <code>wait()</code> will execute the deferred function.
</p>

<p><i>[
2010 Pittsburgh:  Moved to <del>NAD Editorial</del><ins>Resolved</ins>.  Rationale added below.
]</i></p>




<p><b>Rationale:</b></p>
<p>
Solved by
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3058.html">N3058</a>.
</p>


<p id="res-1266"><b>Proposed resolution:</b></p>
<p>
Replace  [futures.shared_future]p22 with the following:
</p>

<blockquote>
<p>
<i>Effects:</i> If the associated state
<del>contains a deferred function, executes the deferred function. Otherwise,
blocks until the associated state is ready.</del>
<ins>was created by a <code>promise</code> or <code>packaged_task</code> object, block
until the associated state is ready. If the associated state is associated with
a thread created for an <code>async</code> call (32.10.9 <a href="https://wg21.link/futures.async">[futures.async]</a>), as
if <code>associated-thread.join()</code>.
</ins></p>

<p><ins>
If the associated state contains a deferred function, calls to <code>wait()</code>
on all <code>shared_future</code> objects that share the same associated state are
serialized. The first call to <code>wait()</code> that shares a given associated
state executes the deferred function and stores the return value or exception in
the associated state.
</ins></p>

<p><ins>
<i>Synchronization:</i> if the associated state was created by a
<code>promise</code> object, the completion of <code>set_value()</code> or
<code>set_exception()</code> to that <code>promise</code> happens before (6.10.2 <a href="https://wg21.link/intro.multithread">[intro.multithread]</a>) <code>wait()</code> returns. If the associated state
was created by a <code>packaged_task</code> object, the completion of the associated
task happens before <code>wait()</code> returns. If the associated state is
associated with a thread created for an <code>async</code> call (32.10.9 <a href="https://wg21.link/futures.async">[futures.async]</a>), the completion of the associated thread happens-before
<code>wait()</code> returns.
</ins></p>

<p><ins>
If the associated state contained a deferred function, the invocation of the
deferred function happens-before any call to <code>wait()</code> on a
<code>future</code> that shares that state returns.
</ins></p>
</blockquote>





</body>
</html>
