<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1518: Waiting for deferred functions</title>
<meta property="og:title" content="Issue 1518: Waiting for deferred functions">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1518.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#C++11">C++11</a> status.</em></p>
<h3 id="1518"><a href="lwg-defects.html#1518">1518</a>. Waiting for deferred functions</h3>
<p><b>Section:</b> 32.10 <a href="https://wg21.link/futures">[futures]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Alberto Ganesh Barbati <b>Opened:</b> 2010-09-14 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#futures">issues</a> in [futures].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p>The current WP N3126 contains ambiguous statements about the
behaviour of functions <code>wait_for</code>/<code>wait_until</code> in
case the future refers to a deferred function. Moreover, I believe
it describes a disputable intent, different from the one contained
in the original async proposals, that may have been introduced
inadvertently during the "async cleanup" that occurred recently.
Consider the following case:</p>
<blockquote>
<pre>
int f();  
future&lt;int&gt; x = async(launch::deferred, f);
future_status s = x.wait_for(chrono::milliseconds(100));
</pre></blockquote>
<p>This example raises two questions:</p>
<ol>
<li>is <code>f</code> invoked?</li>
<li>what is the value of <code>s</code>?</li>
</ol>
<p>According to the current WP, the answer to question 1 is yes,
because 30.6.9/3 says "The first call to a function waiting for the
associated asynchronous state created by this async call to become
ready shall invoke the deferred function in the thread that called
the waiting function". The answer to question 2, however, is not as
clear. According to 30.6.6/23, s should be
<code>future_status::deferred</code> because <code>x</code> refers to a
deferred function that is not running, but it should also be
<code>future_status::ready</code> because after executing <code>f</code>
(and we saw that <code>f</code> is always executed) the state becomes
ready. By the way, the expression "deferred function that is not
running" is very unfortunate in itself, because it may apply to
both the case where the function hasn't yet started, as well as the
case where it was executed and completed.</p>
<p>While we clearly have a defect in the WP answering to question
2, it is my opinion that the answer to question 1 is wrong, which
is even worse. Consider that the execution of the function
<code>f</code> can take an arbitrarily long time. Having
<code>wait_for()</code> invoke <code>f</code> is a potential violation of
the reasonable expectation that the execution of
<code>x.wait_for(chrono::milliseconds(100))</code> shall take <span style="text-decoration:underline">at most</span>
100 milliseconds plus a delay dependent on the quality of implementation
and the quality of management (as described in paper N3128).
In fact, previous versions of the WP
clearly specified that only function <code>wait()</code> is required to
execute the deferred function, while <code>wait_for()</code> and
<code>wait_until()</code> shouldn't.</p>
<p>The proposed resolution captures the intent that
<code>wait_for()</code> and <code>wait_until()</code> should never attempt
to invoke the deferred function. In other words, the P/R provides
the following answers to the two questions above:</p>
<ol>
<li>no</li>
<li><code>future_status::deferred</code></li>
</ol>
<p>In order to simplify the wording, the definition of <i>deferred
function</i> has been tweaked so that the function is no longer
considered deferred once its evaluation has started, as suggested
by Howard.</p>
<p>Discussions in the reflector questioned whether
<code>wait_for()</code> and <code>wait_until()</code> should return
immediately or actually wait hoping for a second thread to execute
the deferred function. I believe that waiting could be useful only
in a very specific scenario but detrimental in the general case and
would introduce another source of ambiguity: should
<code>wait_for()</code> return <code>future_status::deferred</code> or
<code>future_status::timeout</code> after the wait? Therefore the P/R
specifies that <code>wait_for</code>/<code>wait_until</code> shall return
immediately, which is simpler, easier to explain and more useful in
the general case.</p>

<p><i>[
Post-Rapperswil
]</i></p>


<blockquote><p>
Moved to Tentatively Ready after 5 positive votes on c++std-lib.
</p></blockquote>

<p><i>[
Adopted at 2010-11 Batavia
]</i></p>




<p id="res-1518"><b>Proposed resolution:</b></p>

<p>The proposed wording changes are relative to the Final Committee Draft,
N3126.</p>
<p><b>Note to the editor:</b> the proposed wording is meant not be in conflict
with any change proposed by paper N3128 "C++ Timeout Specification".
Ellipsis are deliberately used to avoid any unintended overlapping.</p>
<ol>
<li>
<p>In [futures.unique_future] 30.6.6/22:</p>
<p>Effects: <ins>none if the associated asynchronous state contains
a deferred function (30.6.9), otherwise</ins> blocks until the
associated asynchronous state is ready or [...].</p>
</li>
<li>
<p>In [futures.unique_future] 30.6.6/23 first bullet:</p>
<p>&mdash; future_status::deferred if the associated asynchronous
state contains a deferred function <del>that is not
running</del>.</p>
</li>
<li>
<p>In [futures.unique_future] 30.6.6/25:</p>
<p>Effects: <ins>none if the associated asynchronous state contains
a deferred function (30.6.9), otherwise</ins> blocks until the
associated asynchronous state is ready or [...].</p>
</li>
<li>
<p>In [futures.unique_future] 30.6.6/26 first bullet:</p>
<p>&mdash; future_status::deferred if the associated asynchronous
state contains a deferred function <del>that is not
running</del>.</p>
</li>
<li>
<p>In [futures.shared_future] 30.6.7/27</p>
<p>Effects: <ins>none if the associated asynchronous state contains
a deferred function (30.6.9), otherwise</ins> blocks until the
associated asynchronous state is ready or [...].</p>
</li>
<li>
<p>In [futures.unique_future] 30.6.7/28 first bullet:</p>
<p>&mdash; future_status::deferred if the associated asynchronous
state contains a deferred function <del>that is not
running</del>.</p>
</li>
<li>
<p>In [futures.shared_future] 30.6.6/30:</p>
<p>Effects: <ins>none if the associated asynchronous state contains
a deferred function (30.6.9), otherwise</ins> blocks until the
associated asynchronous state is ready or [...].</p>
</li>
<li>
<p>In [futures.unique_future] 30.6.7/31 first bullet:</p>
<p>&mdash; future_status::deferred if the associated asynchronous
state contains a deferred function <del>that is not
running</del>.</p>
</li>
<li>
<p>In [futures.atomic_future] 30.6.8/23</p>
<p>Effects: <ins>none if the associated asynchronous state contains
a deferred function (30.6.9), otherwise</ins> blocks until the
associated asynchronous state is ready or [...].</p>
</li>
<li>
<p>In [futures.unique_future] 30.6.8/24 first bullet:</p>
<p>&mdash; future_status::deferred if the associated asynchronous
state contains a deferred function <del>that is not
running</del>.</p>
</li>
<li>
<p>In [futures.atomic_future] 30.6.8/27:</p>
<p>Effects: <ins>none if the associated asynchronous state contains
a deferred function (30.6.9), otherwise</ins> blocks until the
associated asynchronous state is ready or [...].</p>
</li>
<li>
<p>In [futures.unique_future] 30.6.8/28 first bullet:</p>
<p>&mdash; future_status::deferred if the associated asynchronous
state contains a deferred function <del>that is not
running</del>.</p>
</li>
<li>
<p>In [futures.async] 30.6.9/3 second bullet:</p>
<p>[...] The first call to a function
<del>waiting</del><ins>requiring a non-timed wait</ins> for the
associated asynchronous state created by this async call to become
ready shall invoke the deferred function in the thread that called
the waiting function; <ins>once evaluation of <code><i>INVOKE</i>(g,
xyz)</code> begins, the function is no longer considered
deferred</ins> <del>all other calls waiting for the same associated
asynchronous state to become ready shall block until the deferred
function has completed</del>.</p>
</li>
</ol>






</body>
</html>
