<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4174: How does [atomics.order] p3 apply when then modification is an initialization?</title>
<meta property="og:title" content="Issue 4174: How does [atomics.order] p3 apply when then modification is an initialization?">
<meta property="og:description" content="C++ library issue. Status: SG1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4174.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#SG1">SG1</a> status.</em></p>
<h3 id="4174"><a href="lwg-active.html#4174">4174</a>. How does [atomics.order] p3 apply when then modification is an initialization?</h3>
<p><b>Section:</b> 32.5.4 <a href="https://wg21.link/atomics.order">[atomics.order]</a> <b>Status:</b> <a href="lwg-active.html#SG1">SG1</a>
 <b>Submitter:</b> jim x <b>Opened:</b> 2024-11-13 <b>Last modified:</b> 2025-02-07</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#atomics.order">active issues</a> in [atomics.order].</p>
<p><b>View all other</b> <a href="lwg-index.html#atomics.order">issues</a> in [atomics.order].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#SG1">SG1</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Consider this example
</p>
<blockquote><pre>
std::atomic&lt;int&gt; v = 0;
// thread 1:
v.load(std::memory_order::seq_cst);
//thread 2:
v.store(1,std::memory_order::seq_cst);
</pre></blockquote>
<p>
If the load operation reads the value <code class='backtick'>0</code>, how are load and store operations ordered in the single total order? 
According to 32.5.4 <a href="https://wg21.link/atomics.order">[atomics.order]</a> p3 (emphasize mine)
</p>
<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;">
<p>
An atomic operation <i>A</i> on some atomic object <i>M</i> is <i>coherence-ordered before</i> 
another atomic operation <i>B</i> on <i>M</i> if
</p>
<ol style="list-style-type: none">
<li><p>[&hellip;]</p></li>
<li><p>(3.3) &mdash; <i>A</i> and <i>B</i> are not the same atomic read-modify-write operation, 
and there exists an <b>atomic modification <i>X</i></b> of <i>M</i> such that <i>A</i> reads the value stored by 
<i>X</i> and <i>X</i> precedes <i>B</i> in the modification order of <i>M</i>, or</p></li>
</ol>
</blockquote>
<p>
According to 32.5.8.2 <a href="https://wg21.link/atomics.types.operations">[atomics.types.operations]</a> p3 (emphasize mine)
</p>
<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;">
<i>Effects</i>: Initializes the object with the value <code>desired</code>. <b>Initialization is not an atomic operation</b> 
(6.10.2 <a href="https://wg21.link/intro.multithread">[intro.multithread]</a>).
</blockquote>
<p>
So, how does 32.5.4 <a href="https://wg21.link/atomics.order">[atomics.order]</a> p3 apply to this example such that the load operation precedes 
the store operation in the single total order <i>S</i>?
</p>

<p><i>[2025-02-07; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll. Send to SG1.
</p>
<p>
LWG found the issue unclear and felt it was missing context that would
help understand it properly.
</p>
<p>
In
<a href="https://github.com/cplusplus/CWG/issues/641">cplusplus/CWG/issues/641</a>
the following example was given:
<blockquote>
<pre>
std::atomic&lt;bool&gt; a = false;
std::atomic&lt;bool&gt; b = false;
int v = 0;
// thread 1:
a.store(true, seq_cst);
if(b.load(seq_cst)== false){
   v = 1;  // #1
}
//thread 2:
b.store(true, seq_cst);
if(a.load(seq_cst)== false){
   v = 2; // #2
}
</pre>
To prove whether #1 and #2 can have data race, we should prove whether
it's possible that <code class='backtick'>a</code> and <code class='backtick'>b</code> simultaneously read <code class='backtick'>false</code>.
This proof equals whether there can be a valid single total order in this case.
To determine the order of <code class='backtick'>b.load</code> and <code class='backtick'>b.store</code> when <code class='backtick'>b.load</code> reads
the initialization value <code class='backtick'>false</code>, 32.5.4 <a href="https://wg21.link/atomics.order">[atomics.order]</a> p3.3
should apply here.
However, the initialization is not an atomic modification
such that <code class='backtick'>X</code> cannot be that value.
</blockquote>
</p>

<p>
A possible fix is to amend 32.5.4 <a href="https://wg21.link/atomics.order">[atomics.order]</a>/3.3 to say something
like this:
<blockquote>
(3.3) A and B are not the same atomic read-modify-write operation, and
<ins>either</ins>
<ol style="list-style-type: none">
<li><ins>(3.3.1)</ins>
there exists an atomic modification X of M such that A reads
the value stored by X and X precedes B in the modification order of M<ins>,
or</ins>
</li>
<li><ins>(3.3.2)
A reads the initial value of X and B modifies M</ins>, or
</li>
</ol>
</blockquote>
</p>



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





</body>
</html>
