<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 893: std::mutex issue</title>
<meta property="og:title" content="Issue 893: std::mutex issue">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue893.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++11">C++11</a> status.</em></p>
<h3 id="893"><a href="lwg-defects.html#893">893</a>. <code>std::mutex</code> issue</h3>
<p><b>Section:</b> 32.6.4.2.2 <a href="https://wg21.link/thread.mutex.class">[thread.mutex.class]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Peter Dimov <b>Opened:</b> 2008-09-15 <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.mutex.class">issues</a> in [thread.mutex.class].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++11">C++11</a> status.</p>
<p><b>Duplicate of:</b> <a href="lwg-closed.html#905" title="Mutex specification questions (Status: Dup)">905</a></p>
<p><b>Discussion:</b></p>
<p>
32.6.4.2.2 <a href="https://wg21.link/thread.mutex.class">[thread.mutex.class]</a>/27 (in
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2723.pdf">N2723</a>)
says that the behavior is undefined if:
</p>
<ul>
<li>a thread that owns a <code>mutex</code> object calls <code>lock()</code> or
<code>try_lock()</code> on that object</li>
</ul>
<p>
I don't believe that this is right. Calling <code>lock()</code> or <code>try_lock()</code> on a
locked <code>mutex</code> is well defined in the general case. <code>try_lock()</code> is required
to fail and return <code>false</code>. <code>lock()</code> is required to either throw an
exception (and is allowed to do so if it detects deadlock) or to block
until the <code>mutex</code> is free. These general requirements apply regardless of
the current owner of the <code>mutex</code>; they should apply even if it's owned by
the current thread.
</p>
<p>
Making double <code>lock()</code> undefined behavior probably can be justified (even
though I'd still disagree with the justification), but <code>try_lock()</code> on a
locked <code>mutex</code> must fail.
</p>

<p><i>[
Summit:
]</i></p>

<blockquote>
<p>
Move to open. Proposed resolution:
</p>
<ul>
<li>
In 32.6.4 <a href="https://wg21.link/thread.mutex.requirements">[thread.mutex.requirements]</a> paragraph 12, change the error
condition for <code>resource_deadlock_would_occur</code> to: "if the implementation
detects that a deadlock would occur"
</li>
<li>
Strike 32.6.4.2.2 <a href="https://wg21.link/thread.mutex.class">[thread.mutex.class]</a> paragraph 3 bullet 2 "a thread that owns a mutex object
calls <code>lock()</code> or <code>try_lock()</code> on that object, or"
</li>
</ul>
</blockquote>

<p><i>[
2009-07 Frankfurt
]</i></p>


<blockquote><p>
Move to Review. Alisdair to provide note.
</p></blockquote>

<p><i>[
2009-07-31 Alisdair provided note.
]</i></p>


<p><i>[
2009-10 Santa Cruz:
]</i></p>


<blockquote><p>
Moved to Ready.
</p></blockquote>

<p><i>[
2009-11-18 Peter Opens:
]</i></p>


<blockquote>
<p>
I don't believe that the proposed note:
</p>

<blockquote><p>
[<i>Note:</i> a program may deadlock if the thread that owns a <code>mutex</code>
object calls <code>lock()</code> or <code>try_lock()</code> on that object. If the program can
detect the deadlock, a <code>resource_deadlock_would_occur</code> error condition may
be observed. &mdash; <i>end note</i>]
</p></blockquote>

<p>
is entirely correct. "or <code>try_lock()</code>" should be removed, because
<code>try_lock</code> is non-blocking and doesn't deadlock; it just returns
<code>false</code> when it fails to lock the mutex.
</p>

<p><i>[
Howard: I've set to Open and updated the wording per Peter's suggestion.
]</i></p>


</blockquote>

<p><i>[
2009-11-18 Moved to Tentatively Ready after 5 positive votes on c++std-lib.
]</i></p>




<p id="res-893"><b>Proposed resolution:</b></p>
<p>
In 32.6.4 <a href="https://wg21.link/thread.mutex.requirements">[thread.mutex.requirements]</a> paragraph 12 change:
</p>

<blockquote>
<ul>
<li>...</li>
<li>
<code>resource_deadlock_would_occur</code> -- if the <del>current thread already owns the mutex and is able 
to detect it</del> <ins>implementation detects that a deadlock would occur</ins>.
</li>
<li>...</li>
</ul>
</blockquote>

<p>
Strike 32.6.4.2.2 <a href="https://wg21.link/thread.mutex.class">[thread.mutex.class]</a> paragraph 3 bullet 2:
</p>
<blockquote>
<p>
-3- The behavior of a program is undefined if:
</p>
<ul>
<li>...</li>
<li>
<del>a thread that owns a <code>mutex</code> object calls <code>lock()</code> or <code>try_lock()</code> on that object, or</del>
</li>
<li>...</li>
</ul>
</blockquote>

<p>
Add the following note after p3 32.6.4.2.2 <a href="https://wg21.link/thread.mutex.class">[thread.mutex.class]</a>
</p>

<blockquote><p>
[<i>Note:</i> a program may deadlock if the thread that owns a <code>mutex</code>
object calls <code>lock()</code> on that object. If the implementation can detect the
deadlock, a <code>resource_deadlock_would_occur</code> error condition may be
observed. &mdash; <i>end note</i>]
</p></blockquote>






</body>
</html>
