<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3288: atomic&lt;T&gt;::notify_one is unimplementable</title>
<meta property="og:title" content="Issue 3288: atomic&lt;T&gt;::notify_one is unimplementable">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3288.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#New">New</a> status.</em></p>
<h3 id="3288"><a href="lwg-active.html#3288">3288</a>. <code>atomic&lt;T&gt;::notify_one</code> is unimplementable</h3>
<p><b>Section:</b> 32.5.6 <a href="https://wg21.link/atomics.wait">[atomics.wait]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Anthony Williams <b>Opened:</b> 2019-09-11 <b>Last modified:</b> 2020-09-06</p>
<p><b>Priority: </b>2
</p>
<p><b>View other</b> <a href="lwg-index-open.html#atomics.wait">active issues</a> in [atomics.wait].</p>
<p><b>View all other</b> <a href="lwg-index.html#atomics.wait">issues</a> in [atomics.wait].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
I am concerned by the wording around <code>atomic&lt;T&gt;::wait()/atomic&lt;T&gt;::notify_one()</code>.
<p/>
32.5.6 <a href="https://wg21.link/atomics.wait">[atomics.wait]</a> p4 requires that the thread that calls <code>wait()</code> observed a
value <code>X</code> prior to the value <code>Y</code> which results from a store that happens-before 
the notify in order to be eligible to be unlocked.
<p/>
I am not sure how to implement that.
</p>
<blockquote><pre>
atomic&lt;int&gt; a = 0;

T1: int ra=a, read 0
T1: a.wait(0)
T2: a=42
T3: int ra=a, read 42
T3: a.wait(42)
T2: a.notify_one()
</pre></blockquote>
<p>
The wording requires that <code>T1</code> is eligible to be unlocked, but not <code>T3</code>, as
there is not a write <em>after</em> the value read by <code>T3</code> that happens-before
the notify.
<p/>
However, both <code>T1</code> and <code>T3</code> are waiting, so <code>T3</code> may be woken by the OS.
Waking <code>T3</code> is allowed (<code>wait()</code> says it may wake spuriously), but waking <code>T1</code>
is currently required as it is the only thread "eligible to be unblocked".
<p/>
This requires <code>notify_one()</code> to wake <em>all</em> waiters, which defeats the purpose.
<p/>
I suspect we need to change 32.5.6 <a href="https://wg21.link/atomics.wait">[atomics.wait]</a> p4.
<p/>
How about:
</p>
<blockquote><p>
"A call to an atomic waiting operation <code>W</code> on an atomic object <code>M</code> is
<i>eligible to be unlocked</i> by a call to an atomic notifying operation <code>N</code> on
<code>M</code> if
<ul>
<li><p><code>N</code> does not happen-before <code>W</code></p></li>
<li><p>There are no side effects <code>X</code> and <code>Y</code> in the modification order of <code>M</code> such
that <code>N</code> happens-before <code>X</code>, <code>X</code> precedes <code>Y</code> in the modification order 
of <code>M</code> and an atomic operation that observes the effects of <code>Y</code> happens-before <code>W</code>.</p></li>
</ul>
"
</p></blockquote>
<p>
This would allow <code>T3</code> to be woken in the preceding example, but prevent it
being woken in the following case:
</p>
<blockquote><pre>
T1: int ra=a, read 0
T1: a.wait(0)
T2: a=42
T2: a.notify_one()
T2: a=69
T3: int ra=a, read 69
T3: a.wait(69)
</pre></blockquote>

<p><i>[2020-07-17; Priority set to 2 in telecon]</i></p>



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

<ol>
<li><p>Modify 32.5.6 <a href="https://wg21.link/atomics.wait">[atomics.wait]</a> as indicated:</p>

<blockquote>
<p>
-4- A call to an atomic waiting operation <ins><code>W</code></ins> on an atomic object <code>M</code> is 
<i>eligible to be unblocked</i> by a call to an atomic notifying operation <ins><code>N</code></ins> on <code>M</code> 
if <del>there exist side effects <code>X</code> and <code>Y</code> on <code>M</code> such that:</del>
<ol style="list-style-type: none">
<li><p>(4.1) &mdash; <ins><code>N</code> does not happen before <code>W</code></ins><del>the atomic waiting operation 
has blocked after observing the result of <code>X</code></del>,</p></li>
<li><p>(4.2) &mdash; <ins>There are no side effects</ins> <code>X</code> <ins>and</ins><del>precedes</del> <code>Y</code> 
in the modification order of <code>M</code><del>, and</del><ins>such that <code>N</code> happens before <code>X</code>, 
<code>X</code> precedes <code>Y</code> in the modification order of <code>M</code> and an atomic operation that observes 
the effects of <code>Y</code> happens before <code>W</code>.</ins></p></li>
<li><p><del>(4.3) &mdash; <code>Y</code> happens before the call to the atomic notifying operation.</del></p></li>
</ol>
</p>
</blockquote>
</li>

</ol>




</body>
</html>
