<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2532: Satisfying a promise at thread exit</title>
<meta property="og:title" content="Issue 2532: Satisfying a promise at thread exit">
<meta property="og:description" content="C++ library issue. Status: Open">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2532.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#Open">Open</a> status.</em></p>
<h3 id="2532"><a href="lwg-active.html#2532">2532</a>. Satisfying a <code>promise</code> at thread exit</h3>
<p><b>Section:</b> 32.10.6 <a href="https://wg21.link/futures.promise">[futures.promise]</a> <b>Status:</b> <a href="lwg-active.html#Open">Open</a>
 <b>Submitter:</b> Agust&iacute;n K-ballo Berg&eacute; <b>Opened:</b> 2015-09-03 <b>Last modified:</b> 2016-08-06</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#futures.promise">issues</a> in [futures.promise].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Open">Open</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>promise::set_value_at_thread_exit</code> and <code>promise::set_exception_at_thread_exit</code> operate on a shared state 
at thread exit, without making the thread participate in the ownership of such shared state.
<p/>
Consider the following snippet:
</p>
<blockquote><pre>
std::promise&lt;int&gt;{}.set_value_at_thread_exit(42);
</pre></blockquote>
<p>
Arguably, since the <code>promise</code> abandons its shared state without actually making it ready, a <code>broken_promise</code> 
error condition should be stored in the shared state. Implementations diverge, they either crash at thread exit by 
dereferencing an invalid pointer, or keep the shared state around until thread exit.
</p>

<p><i>[2016-08-03 Chicago]</i></p>

<p>This is related to <a href="lwg-active.html#2530" title="Clarify observable side effects of releasing a shared state (Status: Open)">2530</a><sup><a href="https://cplusplus.github.io/LWG/issue2530" title="Latest snapshot">(i)</a></sup></p>
<p><i>[2016-08-03, Billy O'Neal suggests concrete wording]</i></p>

<p>Fri AM: Moved to Open</p>


<p id="res-2532"><b>Proposed resolution:</b></p>
<p>
This wording is relative to N4606.
</p>

<ol>
<li><p>Change 32.10.5 <a href="https://wg21.link/futures.state">[futures.state]</a> p7 as indicated:</p>

<blockquote>
<p>
-7- When an asynchronous provider is said to abandon its shared state, it means:
</p>
<ol style="list-style-type: none">
<li><p>(7.1) &mdash; first, if that state is not ready <ins>or scheduled to be made ready at thread exit</ins>, the provider</p>
<ol style="list-style-type: none">
<li><p>(7.1.1) &mdash; stores an exception object of type <code>future_error</code> with an error condition of 
<code>broken_promise</code> within its shared state; and then</p></li>
<li><p>(7.1.2) &mdash; makes its shared state ready;</p></li>
</ol>
</li>
</ol>
</blockquote>
</li>

<li><p>Change 32.10.5 <a href="https://wg21.link/futures.state">[futures.state]</a> p10 as indicated:</p>

<blockquote>
<p>
-10- Some functions (e.g., <code>promise::set_value_at_thread_exit</code>) <del>delay making the shared state ready 
until</del><ins>schedule the shared state to be made ready when</ins> the
calling thread exits. <ins>This associates a reference to the shared state with the calling thread.</ins> The 
destruction of each of that thread's objects with thread storage duration 
(6.8.6.3 <a href="https://wg21.link/basic.stc.thread">[basic.stc.thread]</a>) is sequenced before making that shared state ready. <ins>When the calling 
thread makes the shared state ready, if the thread holds the last reference to the shared state, the shared state 
is destroyed. [<i>Note:</i> This means that the shared state may not become ready until after the <em>asynchronous 
provider</em> has been destroyed. &mdash; <i>end note</i>]</ins>
</p>
</blockquote>
</li>
</ol>





</body>
</html>
