<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3611: Should compare_exchange be allowed to modify the expected value on success?</title>
<meta property="og:title" content="Issue 3611: Should compare_exchange be allowed to modify the expected value on success?">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3611.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#NAD">NAD</a> status.</em></p>
<h3 id="3611"><a href="lwg-closed.html#3611">3611</a>. Should <code>compare_exchange</code> be allowed to modify the <code>expected</code> value on success?</h3>
<p><b>Section:</b> 32.5.8.2 <a href="https://wg21.link/atomics.types.operations">[atomics.types.operations]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2021-09-29 <b>Last modified:</b> 2022-08-23</p>
<p><b>Priority: </b>2
</p>
<p><b>View other</b> <a href="lwg-index-open.html#atomics.types.operations">active issues</a> in [atomics.types.operations].</p>
<p><b>View all other</b> <a href="lwg-index.html#atomics.types.operations">issues</a> in [atomics.types.operations].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Currently a <code>compare_exchange</code> will only update the <code>expected</code> parameter if the
<code>compare_exchange</code> fails. This precludes unconditionally clearing the padding bits of the
<code>expected</code> object prior to doing the <code>compare_exchange</code>, which complicates the
implementation by requiring additional work (e.g. making a copy of the <code>expected</code> value
and clearing the copy's padding, then copying it back to expected only if the <code>compare_exchange</code> fails).
<p/>
We should consider whether we want to allow modifications to <code>expected</code> in the success case,
if such modifications only affect padding bits (i.e. do not alter the value). If we want to allow it,
we need to say so explicitly. The current wording does not permit modifications in the success case,
and any such modification could create a data race if another thread is reading the same memory location
(if it knows a priori that a <code>compare_exchange_strong</code> would succeed and so not write to that location).
</p>

<p><i>[2021-10-14; Reflector poll]</i></p>

<p>
Set priority to 2 after reflector poll. Send to SG1.
</p>
<p>
This is <a href="lwg-defects.html#2426" title="Issue about compare_exchange (Status: C++17)">2426</a><sup><a href="https://cplusplus.github.io/LWG/issue2426" title="Latest snapshot">(i)</a></sup> again,
but the new requirement to clear padding bits changes things.
</p>

<p><i>[2022-07-06; Move to "Open" following SG1 feedback]</i></p>

<p>
Allow compare_exchange to modify the expected value on success?
<table>
<tr><td>SF</td><td>F</td><td>N</td><td>A</td><td>SA</td></tr>
<tr><td>0</td><td>0</td><td>0</td><td>5</td><td>4</td></tr>
</table>
</p>

<p><i>[2022-07-14; Move to "Tentatively NAD" following reflector poll]</i></p>


<p><i>[2022-08-23 Status changed: Tentatively NAD &rarr; NAD.]</i></p>



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





</body>
</html>
