<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3788: jthread::operator=(jthread&amp;&amp;) postconditions are unimplementable under self-assignment</title>
<meta property="og:title" content="Issue 3788: jthread::operator=(jthread&amp;&amp;) postconditions are unimplementable under self-assignment">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3788.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++23">C++23</a> status.</em></p>
<h3 id="3788"><a href="lwg-defects.html#3788">3788</a>. <code>jthread::operator=(jthread&amp;&amp;)</code> postconditions are unimplementable under self-assignment</h3>
<p><b>Section:</b> 32.4.4.2 <a href="https://wg21.link/thread.jthread.cons">[thread.jthread.cons]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Nicole Mazzuca <b>Opened:</b> 2022-09-22 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>3
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++23">C++23</a> status.</p>
<p><b>Discussion:</b></p>
<p>
In the <i>Postconditions</i> element of <code>jthread&amp; jthread::operator=(jthread&amp;&amp;)</code> 
(32.4.4.2 <a href="https://wg21.link/thread.jthread.cons">[thread.jthread.cons]</a> p13), we have the following:
</p>
<blockquote><p>
<i>Postconditions</i>: <code>x.get_id() == id()</code>, and <code>get_id()</code> returns the value of
<code>x.get_id()</code> prior to the assignment. <code>ssource</code> has the value of <code>x.ssource</code> 
prior to the assignment and <code>x.ssource.stop_possible()</code> is <code>false</code>.
</p></blockquote>
<p>
Assume <code>j</code> is a joinable <code>jthread</code>. Then, <code>j = std::move(j);</code> results in the following post-conditions:
</p>
<ul>
<li><p>Let <code>old_id = j.get_id()</code> (and since <code>j</code> is joinable, <code>old_id != id()</code>)</p></li>
<li><p>Since <code>x.get_id() == id()</code>, <code>j.get_id() == id()</code></p></li>
<li><p>Since <code>get_id() == old_id</code>, <code>j.get_id() == old_id</code></p></li>
<li><p>Thus, <code>id() == j.get_id() == old_id</code>, and <code>old_id != id()</code>, which is a contradiction.</p></li>
</ul>
<p>
One can see that these postconditions are therefore unimplementable.
<p/>
Two standard libraries &ndash; the MSVC STL and libstdc++ &ndash; currently implement <code>jthread</code>.
<p/>
The MSVC STL chooses to follow the letter of the <i>Effects</i> element, which results in unfortunate behavior.
It first <code>request_stop()</code>s, then <code>join()</code>s; then, it assigns the values over. This results in
<code>j.get_id() == id()</code> &ndash; this means that <code>std::swap(j, j)</code> stops and joins <code>j</code>.
<p/>
libstdc++ chooses instead to implement this move assignment operator via the move-swap idiom.
This results in <code>j.get_id() == old_id</code>, and <code>std::swap(j, j)</code> is a no-op.
<p/>
It is the opinion of the issue writer that libstdc++'s choice is the correct one, and should be
taken into the standard.
</p>

<p><i>[2022-09-28; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
</p>

<p><i>[2022-09-28; Jonathan provides wording]</i></p>


<p><i>[2022-09-29; Reflector poll]</i></p>

<p>
Set status to Tentatively Ready after five votes in favour during reflector poll.
</p>


<p><i>[2022-11-12 Approved at November 2022 meeting in Kona. Status changed: Voting &rarr; WP.]</i></p>



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

<p>
This wording is relative to <a href="https://wg21.link/N4917" title=" Working Draft, Standard for Programming Language C++">N4917</a>.
</p>

<ol>
<li>
<p>Modify 32.4.4.2 <a href="https://wg21.link/thread.jthread.cons">[thread.jthread.cons]</a> as indicated:</p>

<blockquote>
<pre>jthread&amp; operator=(jthread&amp;&amp; x) noexcept;</pre>
<blockquote>
<p>-12- <i>Effects</i>:
If
<ins><code>&amp;x == this</code> is <code>true</code>, there are no effects.
Otherwise, if</ins>
<code>joinable()</code> is <code>true</code>, calls <code>request_stop()</code>
and then <code>join()</code><del>.
Assigns</del><ins>, then assigns</ins>
the state of <code>x</code> to <code>*this</code>
and sets <code>x</code> to a default constructed state.
</p>
<p> -13- <i>Postconditions</i>:
<del><code>x.get_id() == id()</code> and</del>
<code>get_id()</code> returns the value of <code>x.get_id()</code> prior to the assignment.
<code>ssource</code> has the value of <code>x.ssource</code> prior to the assignment
<del>and <code>x.ssource.stop_possible()</code> is <code>false</code></del>.
</p>
<p>-14- <i>Returns</i>: <code>*this</code>.</p>
</blockquote>
</blockquote>
</li>
</ol>






</body>
</html>
