<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2023: Incorrect requirements for lock_guard and unique_lock</title>
<meta property="og:title" content="Issue 2023: Incorrect requirements for lock_guard and unique_lock">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2023.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#Resolved">Resolved</a> status.</em></p>
<h3 id="2023"><a href="lwg-defects.html#2023">2023</a>. Incorrect requirements for <code>lock_guard</code> and <code>unique_lock</code></h3>
<p><b>Section:</b> 32.6.5.2 <a href="https://wg21.link/thread.lock.guard">[thread.lock.guard]</a>, 32.6.5.4 <a href="https://wg21.link/thread.lock.unique">[thread.lock.unique]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2010-12-08 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#thread.lock.guard">issues</a> in [thread.lock.guard].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
There are two different <code>*Lockable</code> requirements imposed on template arguments
of the class template <code>lock_guard</code> as of 32.6.5.2 <a href="https://wg21.link/thread.lock.guard">[thread.lock.guard]</a> p. 1+2:
</p>
<blockquote><p>
1 [..] The supplied <code>Mutex</code> type shall meet the <code>BasicLockable</code> requirements (30.2.5.2).
</p></blockquote>

<blockquote><p>
2 The supplied <code>Mutex</code> type shall meet the <code>Lockable</code> requirements (30.2.5.3).
</p></blockquote>
<p>
The <code>Lockable</code> requirements include the availability of a member function <code>try_lock()</code>,
but there is no operational semantics in the specification of <code>lock_guard</code> that would rely
on such a function. It seems to me that paragraph 2 should be removed.
<p/>
Similarly, 32.6.5.4 <a href="https://wg21.link/thread.lock.unique">[thread.lock.unique]</a> p. 1+2 refer to exactly the same two requirements.
In this case it seems as if the intention was that the template arguement <code>Mutex</code> should
always provide the <code>try_lock()</code> member function, because several member functions of
<code>unique_lock</code> (<code>unique_lock(mutex_type&amp; m, try_to_lock_t)</code> or <code>bool try_lock()</code>) 
take advantage of such a function without adding extra requirements for this.
It seems that the requirement subset <code>BasicLockable</code> should be removed.
<p/>
I searched for further possible misusages of the <code>*Lockable</code> requirements, but could not
find any more.
</p>

<p><i>[2011-02-23]</i></p>


<p>Howard suggests an alternative approach in regard to <code>unique_lock</code>: The current
minimum requirements on its template argument should better be reduced to <code>BasicLockable</code> 
instead of the current <code>Lockable</code>, including ammending member-wise constraints where required. 
This suggestions was supported by Anthony, Daniel, and Pablo.
<p/>
Daniel drafts wording that follows this separation strategy.
</p>

<p><i>[2011-02-24 Reflector discussion]</i></p>

<p>
Moved to Tentatively Ready after 5 votes.
</p> 

<blockquote class="note">
<p>
The suggested wording changes are against the working draft N3242.
</p>

<ol>
<li>
<p>Remove 32.6.5.2 <a href="https://wg21.link/thread.lock.guard">[thread.lock.guard]</a> p. 2 completely:</p>

<blockquote><p>
<del>2 The supplied <code>Mutex</code> type shall meet the <code>Lockable</code> requirements (30.2.5.3).</del>
</p></blockquote>

</li>

<li>
<p>Change 32.6.5.4 <a href="https://wg21.link/thread.lock.unique">[thread.lock.unique]</a> p. 1-3 as indicated. The intend is to make <code>BasicLockable</code>
the fundamental requirement for <code>unique_lock</code>. We also update the note to reflect these changes and
synchronize one remaining reference of 'mutex' by the proper term 'lockable object' in sync
to the wording changes of <code>lock_guard</code>:</p>

<blockquote><p>
1 [..] The behavior of a program is undefined if the contained pointer <code>pm</code> is not null
and the <del>mutex</del><ins>lockable object</ins> pointed to by <code>pm</code> does not exist for the entire remaining lifetime (3.8) 
of the <code>unique_lock</code> object. The supplied <code>Mutex</code> type shall meet the 
<code>BasicLockable</code> requirements (30.2.5.2). <del>[Editor's note:
BasicLockable is redundant, since the following additional paragraph requires Lockable.]</del>
</p></blockquote>

<blockquote><p>
<del>2 The supplied <code>Mutex</code> type shall meet the <code>Lockable</code> requirements (30.2.5.3).</del>
</p></blockquote>

<blockquote><p>
3 [ <i>Note</i>: <code>unique_lock&lt;Mutex&gt;</code> meets the <code><ins>Basic</ins>Lockable</code> requirements. If 
<ins><code>Mutex</code> meets the <code>Lockable</code> requirements ([thread.req.lockable.req]), <code>unique_lock&lt;Mutex&gt;</code> 
also meets the <code>Lockable</code> requirements and if</ins> <code>Mutex</code> meets the <code>TimedLockable</code> 
requirements (30.2.5.4), <code>unique_lock&lt;Mutex&gt;</code> also meets the <code>TimedLockable</code> 
requirements. &mdash; <i>end note</i> ]
</p></blockquote>

</li>

<li><p>Modify 32.6.5.4.2 <a href="https://wg21.link/thread.lock.unique.cons">[thread.lock.unique.cons]</a> to add the now necessary member-wise
additional constraints for <code>Lockable</code>:</p>

<blockquote><pre>
unique_lock(mutex_type&amp; m, try_to_lock_t) noexcept;
</pre><blockquote><p>
8 <i>Requires</i>: <ins>The supplied <code>Mutex</code> type shall meet the <code>Lockable</code> requirements ([thread.req.lockable.req]).</ins> 
If <code>mutex_type</code> is not a recursive mutex the calling thread does not own the mutex.
<p/>
9 <i>Effects</i>: Constructs an object of type <code>unique_lock</code> and calls <code>m.try_lock()</code>.
</p></blockquote></blockquote>
</li>

<li><p>Modify 32.6.5.4.3 <a href="https://wg21.link/thread.lock.unique.locking">[thread.lock.unique.locking]</a> to add the now necessary member-wise
additional constraints for <code>Lockable</code>:</p>

<blockquote><pre>
bool try_lock();
</pre><blockquote><p>
<ins>? <i>Requires</i>: The supplied <code>Mutex</code> type shall meet the <code>Lockable</code> requirements ([thread.req.lockable.req]).</ins> 
<p/>
4 <i>Effects</i>: <code>pm-&gt;try_lock()</code>
</p></blockquote></blockquote>
</li>
</ol>
</blockquote>


<p id="res-2023"><b>Proposed resolution:</b></p><p>
Resolved 2011-03 Madrid meeting by paper <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3278">N3278</a>
</p>




</body>
</html>
