<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4172: unique_lock self-move-assignment is broken</title>
<meta property="og:title" content="Issue 4172: unique_lock self-move-assignment is broken">
<meta property="og:description" content="C++ library issue. Status: WP">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4172.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#WP">WP</a> status.</em></p>
<h3 id="4172"><a href="lwg-defects.html#4172">4172</a>. unique_lock self-move-assignment is broken</h3>
<p><b>Section:</b> 32.6.5.4.2 <a href="https://wg21.link/thread.lock.unique.cons">[thread.lock.unique.cons]</a>, 32.6.5.5.2 <a href="https://wg21.link/thread.lock.shared.cons">[thread.lock.shared.cons]</a> <b>Status:</b> <a href="lwg-active.html#WP">WP</a>
 <b>Submitter:</b> Casey Carter <b>Opened:</b> 2024-11-13 <b>Last modified:</b> 2025-02-16</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#thread.lock.unique.cons">issues</a> in [thread.lock.unique.cons].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#WP">WP</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The postconditions in 32.6.5.4.2 <a href="https://wg21.link/thread.lock.unique.cons">[thread.lock.unique.cons]</a> paragraph 19:

<blockquote>
<i>Postconditions</i>:
<code class='backtick'>pm == u_p.pm</code> and <code class='backtick'>owns == u_p.owns</code>
(where <code class='backtick'>u_p</code> is the state of <code class='backtick'>u</code> just prior to this construction),
<code class='backtick'>u.pm == 0</code> and <code class='backtick'>u.owns == false</code>.
</blockquote>

contradict themselves if <code class='backtick'>*this</code> and the parameter <code class='backtick'>u</code> refer to the same object.
(Presumably "this construction" means the assignment, and it is copy-pasta from
the move constructor postconditions.) Apparently
<code class='backtick'>unique_lock</code> didn't get the memo that we require well-defined behavior
from self-move-assignment as of LWG <a href="lwg-defects.html#2839" title="Self-move-assignment of library types, again (Status: C++23)">2839</a><sup><a href="https://cplusplus.github.io/LWG/issue2839" title="Latest snapshot">(i)</a></sup>.
</p>
<p>
Also, the move assignment operator doesn't specify what it returns.
</p>

<p><i>[2024-11-18; Casey expands the PR to cover <code class='backtick'>shared_lock</code>]</i></p>

<p>
<code class='backtick'>shared_lock</code> has the same problems, and can be fixed in the same way.
</p>

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

<p>
Set status to Tentatively Ready after seven votes in favour during reflector poll.
</p>
<p>
"Should use parentheses not braces for the initializations."
Jonathan volunteers to do that editorially after this gets approved.
</p>

<p><i>[Hagenberg 2025-02-16; Status changed: Voting &rarr; WP.]</i></p>



<p id="res-4172"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4993" title=" Working Draft, Programming Languages — C++">N4993</a>.
</p>

<blockquote class="note">
Drafting Note: I've chosen to use the move-into-temporary-and-swap idiom here
to keep things short and sweet.
Since move construction, swap, and destruction are all <code class='backtick'>noexcept</code>,
I've promoted move assignment from "<i>Throws</i>: Nothing" to <code class='backtick'>noexcept</code> as well.
This is consistent with eliminating the implicit narrow contract condition
that <code class='backtick'>*this</code> and <code class='backtick'>u</code> refer to distinct objects.
</blockquote>

<ol>
<li>
<p>
In the class synopsis in 32.6.5.4.1 <a href="https://wg21.link/thread.lock.unique.general">[thread.lock.unique.general]</a>,
annotate the move assignment operator as <code class='backtick'>noexcept</code>:
</p>

<blockquote><pre><code>
  namespace std {
    template&lt;class Mutex&gt;
    class unique_lock {
      [...]
      unique_lock&amp; operator=(unique_lock&amp;&amp; u) <ins>noexcept</ins>;
      [...]
    };
  }
</code></pre></blockquote>
</li>

<li>
<p>
Modify 32.6.5.4.2 <a href="https://wg21.link/thread.lock.unique.cons">[thread.lock.unique.cons]</a> as follows:
</p>

<blockquote>
<pre><code>
unique_lock&amp; operator=(unique_lock&amp;&amp; u) <ins>noexcept</ins>;
</code></pre>
<p>
-18- <i>Effects</i>:
<del>If <code class='backtick'>owns</code> calls <code class='backtick'>pm-&gt;unlock()</code>.</del>
<ins>Equivalent to: <code class='backtick'>unique_lock{std::move(u)}.swap(*this)</code>.</ins>
</p>
<p>
<ins>-?- <i>Returns</i>: <code class='backtick'>*this</code>.</ins>
</p>
<p>
<del>-19- <i>Postconditions</i>:
<code class='backtick'>pm == u_p.pm</code> and <code class='backtick'>owns == u_p.owns</code>
(where <code class='backtick'>u_p</code> is the state of <code class='backtick'>u</code> just prior to this construction),
<code class='backtick'>u.pm == 0</code> and <code class='backtick'>u.owns == false</code>.
</del>
</p>
<p>
<del>-20- [<i>Note 1</i>:
With a recursive mutex it is possible for both <code class='backtick'>*this</code> and u to own
the same mutex before the assignment.
In this case, *this will own the mutex after the assignment and u will not.
&mdash; <i>end note</i>]</del>
</p>
<p>
<del>-21- Throws: Nothing.</del>
</p>
</blockquote>
</li>

<li>
<p>
Modify 32.6.5.5.2 <a href="https://wg21.link/thread.lock.shared.cons">[thread.lock.shared.cons]</a> as follows:
</p>

<blockquote>
<pre><code>
shared_lock&amp; operator=(shared_lock&amp;&amp; sl) noexcept;
</code></pre>
<p>
-17- <i>Effects</i>:
<del>If <code class='backtick'>owns</code> calls <code class='backtick'>pm-&gt;unlock_shared()</code>.</del>
<ins>Equivalent to: <code class='backtick'>shared_lock{std::move(sl)}.swap(*this)</code>.</ins>
</p>
<p>
<ins>-?- <i>Returns</i>: <code class='backtick'>*this</code>.</ins>
</p>
<p>
<del>-18- <i>Postconditions</i>:
<code class='backtick'>pm == sl_p.pm</code> and <code class='backtick'>owns == sl_p.owns</code>
(where <code class='backtick'>sl_p</code> is the state of <code class='backtick'>sl</code> just prior to this assignment),
<code class='backtick'>sl.pm == nullptr</code> and <code class='backtick'>sl.owns == false</code>.
</del>
</p>
</blockquote>
</li>
</ol>






</body>
</html>
