<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2412: promise::set_value() and promise::get_future() should not race</title>
<meta property="og:title" content="Issue 2412: promise::set_value() and promise::get_future() should not race">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2412.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++20">C++20</a> status.</em></p>
<h3 id="2412"><a href="lwg-defects.html#2412">2412</a>. <code>promise::set_value()</code> and <code>promise::get_future()</code> should not race</h3>
<p><b>Section:</b> 32.10.6 <a href="https://wg21.link/futures.promise">[futures.promise]</a>, 32.10.10.2 <a href="https://wg21.link/futures.task.members">[futures.task.members]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2014-06-23 <b>Last modified:</b> 2021-02-25</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#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The following code has a data race according to the standard:
</p>
<blockquote>
<pre>
std::promise&lt;void&gt; p;
std::thread t{ []{
  p.get_future().wait();
}};
p.set_value();
t.join();
</pre>
</blockquote>
<p>
The problem is that both <code>promise::set_value()</code> and
<code>promise::get_future()</code> are non-const member functions which modify the
same object, and we only have wording saying that the <code>set_value()</code> and
<code>wait()</code> calls (i.e. calls setting and reading the shared state) are
synchronized.
<p/>
The calls don't actually access the same memory locations, so the
standard should allow it. My suggestion is to state that calling
<code>get_future()</code> does not conflict with calling the various functions that
make the shared state ready, but clarify with a note that this does
not imply any synchronization or "happens before", only being free
from data races.
</p>

<p><i>[2015-02 Cologne]</i></p>

<p>
Handed over to SG1.
</p>

<p><i>[2016-10-21, Nico comments]</i></p>

<p>
After creating a promise or packaged task one thread can call <code>get_future()</code>
while another thread can set values/exceptions (either directly or via function call).
This happens very easily.
<p/>
Consider:
</p>
<blockquote><pre>
promise&lt;string&gt; p;
thread t(doSomething, ref(p));
cout &lt;&lt; "result: " &lt;&lt; p.get_future().get() &lt;&lt; endl;
</pre></blockquote>
<p>
AFAIK, this is currently UB due to a data race (calling <code>get_future()</code> for the 
promise might happen while setting the value in the promise).
<p/>
Yes, a fix is pretty easy:
</p>
<blockquote><pre>
promise&lt;string&gt; p;
future&lt;string&gt; f(p.get_future());
thread t(doSomething, ref(p));
cout &lt;&lt; "result: " &lt;&lt; f.get() &lt;&lt; endl;
</pre></blockquote>
<p>
but I would like to have <code>get_future()</code> and setters be synchronized to avoid this UB.
<p/>
This would especially make the use of packaged tasks a lot easier. Consider:
</p>
<blockquote><pre>
vector&lt;packaged_task&lt;int(char)&gt;&gt; tasks;
packaged_task&lt;int(char)&gt; t1(func);

// start separate thread to run all tasks:
auto futCallTasks = async(launch::async, callTasks, ref(tasks));

for (auto&amp; fut : tasksResults) {
  cout &lt;&lt; "result: " &lt;&lt; fut.get_future().get() &lt;&lt; endl; // OOPS: UB
}
</pre></blockquote>
<p>
Again, AFAIK, this program currently is UB due to a data race.
Instead, currently I'd have to program, which is a lot less convenient:
</p>
<blockquote><pre>
vector&lt;packaged_task&lt;int(char)&gt;&gt; tasks;
vector&lt;future&lt;int&gt;&gt; tasksResults;
packaged_task&lt;int(char)&gt; t1(func);
tasksResults.push_back(t1.getFuture()));
tasks.push_back(move(t1));

// start separate thread to run all tasks:
auto futCallTasks = async(launch::async, callTasks, ref(tasks));

for (auto&amp; fut : tasksResults) {
  cout &lt;&lt; "result: " &lt;&lt; fut.get() &lt;&lt; endl;
}
</pre></blockquote>
<p>
With my naive thinking I see not reason not to guarantee
that these calls synchronize (as <code>get_future</code> returns an "address/reference"
while all setters set the values there).
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>This wording is relative to N3936.</p>

<ol>
<li><p>Change 32.10.6 <a href="https://wg21.link/futures.promise">[futures.promise]</a> around p12 as indicated:</p>

<blockquote>
<pre>
future&lt;R&gt; get_future();
</pre>
<blockquote>
<p>
-12- <i>Returns</i>: A <code>future&lt;R&gt;</code> object with the same shared state as <code>*this</code>.
<p/>
<ins>-?- <i>Synchronization</i>: Calls to this function do not conflict (6.10.2 <a href="https://wg21.link/intro.multithread">[intro.multithread]</a>) 
with calls to <code>set_value</code>, <code>set_exception</code>, <code>set_value_at_thread_exit</code>, or
<code>set_exception_at_thread_exit</code>. [<i>Note</i>: Such calls need not be synchronized, but implementations 
must ensure they do not introduce data races. &mdash; <i>end note</i>]</ins>
<p/>
-13- <i>Throws</i>: <code>future_error</code> if <code>*this</code> has no shared state or if <code>get_future</code> has already been called on a
<code>promise</code> with the same shared state as <code>*this</code>.
<p/>
-14- <i>Error conditions</i>: [&hellip;]
</p>
</blockquote>
</blockquote>
</li>

<li><p>Change 32.10.10.2 <a href="https://wg21.link/futures.task.members">[futures.task.members]</a> around p13 as indicated:</p>

<blockquote>
<pre>
future&lt;R&gt; get_future();
</pre>
<blockquote>
<p>
-13- <i>Returns</i>: A <code>future&lt;R&gt;</code> object that shares the same shared state as <code>*this</code>.
<p/>
<ins>-?- <i>Synchronization</i>: Calls to this function do not conflict (6.10.2 <a href="https://wg21.link/intro.multithread">[intro.multithread]</a>) 
with calls to <code>operator()</code> or <code>make_ready_at_thread_exit</code>. [<i>Note</i>: Such calls need not be 
synchronized, but implementations must ensure they do not introduce data races. &mdash; <i>end note</i>]</ins>
<p/>
-14- <i>Throws</i>: a <code>future_error</code> object if an error occurs.
<p/>
-15- <i>Error conditions</i>: [&hellip;]
</p>
</blockquote>
</blockquote>
</li>

</ol>
</blockquote>

<p><i>[2017-02-28, Kona]</i></p>

<p>
SG1 has updated wording for LWG 2412. SG1 voted to move this to Ready status by unanimous consent.
</p>

<p><i>[2017-03-01, Kona, SG1]</i></p>

<p>
GeoffR to forward revised wording.
</p>

<p><i>[2018-06, Rapperswil, Wednesday evening session]</i></p>

<p>
JW: lets move on and I'll file another issue to make the wording better<br/>
BO: the current wording is better than what there before<br/>
JM: ACTION I should file an editorial issue to clean up on how to refer to [res.on.data.races]: raised editorial issue 2097<br/>
ACTION: move to Ready
<p/>
Daniel rebases wording to N4750.
</p>
<p><i>[2018-11, Adopted in San Diego]</i></p>



<p id="res-2412"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/n4750">N4750</a>.</p>

<ol>
<li><p>Change 32.10.6 <a href="https://wg21.link/futures.promise">[futures.promise]</a> around p12 as indicated:</p>

<blockquote>
<pre>
future&lt;R&gt; get_future();
</pre>
<blockquote>
<p>
-12- <i>Returns</i>: A <code>future&lt;R&gt;</code> object with the same shared state as <code>*this</code>.
<p/>
<ins>-?- <i>Synchronization</i>: Calls to this function do not introduce data races (6.10.2 <a href="https://wg21.link/intro.multithread">[intro.multithread]</a>) with 
calls to <code>set_value</code>, <code>set_exception</code>, <code>set_value_at_thread_exit</code>, or <code>set_exception_at_thread_exit</code>. 
[<i>Note</i>: Such calls need not synchronize with each other. &mdash; <i>end note</i>]</ins>
<p/>
-13- <i>Throws</i>: <code>future_error</code> if <code>*this</code> has no shared state or if <code>get_future</code> has already been called on a
<code>promise</code> with the same shared state as <code>*this</code>.
<p/>
-14- <i>Error conditions</i>: [&hellip;]
</p>
</blockquote>
</blockquote>
</li>

<li><p>Change 32.10.10.2 <a href="https://wg21.link/futures.task.members">[futures.task.members]</a> around p13 as indicated:</p>

<blockquote>
<pre>
future&lt;R&gt; get_future();
</pre>
<blockquote>
<p>
-13- <i>Returns</i>: A <code>future</code> object that shares the same shared state as <code>*this</code>.
<p/>
<ins>-?- <i>Synchronization</i>: Calls to this function do not introduce data races (6.10.2 <a href="https://wg21.link/intro.multithread">[intro.multithread]</a>) with calls 
to <code>operator()</code> or <code>make_ready_at_thread_exit</code>. [<i>Note</i>: Such calls need not synchronize with each other. 
&mdash; <i>end note</i>]</ins>
<p/>
-14- <i>Throws</i>: a <code>future_error</code> object if an error occurs.
<p/>
-15- <i>Error conditions</i>: [&hellip;]
</p>
</blockquote>
</blockquote>
</li>

</ol>





</body>
</html>
