<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 966: Various threading bugs #16</title>
<meta property="og:title" content="Issue 966: Various threading bugs #16">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue966.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="966"><a href="lwg-defects.html#966">966</a>. Various threading bugs #16</h3>
<p><b>Section:</b> 32.7.4 <a href="https://wg21.link/thread.condition.condvar">[thread.condition.condvar]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Pete Becker <b>Opened:</b> 2009-01-07 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#thread.condition.condvar">active issues</a> in [thread.condition.condvar].</p>
<p><b>View all other</b> <a href="lwg-index.html#thread.condition.condvar">issues</a> in [thread.condition.condvar].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
32.7.4 <a href="https://wg21.link/thread.condition.condvar">[thread.condition.condvar]</a>:
<code>condition_variable::wait</code> and
<code>condition_variable::wait_until</code> both have a postcondition that
<code>lock</code> is locked by the calling thread, and a throws clause that
requires throwing an exception if this postcondition cannot be achieved.
How can the implementation detect that this <code>lock</code> can never be
obtained?
</p>

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


<blockquote><p>
Move to open. Requires wording. Agreed this is an issue, and the
specification should not require detecting deadlocks.
</p></blockquote>

<p><i>[
2009-08-01 Howard provides wording.
]</i></p>


<blockquote>
<p>
The proposed wording is inspired by the POSIX spec which says:
</p>

<blockquote>
<dl>
<dt>[EINVAL]</dt>
<dd>The value specified by cond or mutex is invalid.</dd>
<dt>[EPERM]</dt>
<dd>The mutex was not owned by the current thread at the time of the call.</dd>
</dl>
</blockquote>

<p>
I do not believe [EINVAL] is possible without memory corruption (which we don't
specify).  [EPERM] is possible if this thread doesn't own the mutex, which is
listed as a precondition.  "May" is used instead of "Shall" because not all
OS's are POSIX.
</p>
</blockquote>

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


<blockquote><p>
Leave open, Detlef to provide improved wording.
</p></blockquote>

<p><i>[
2009-10-23 Detlef Provided wording.
]</i></p>


<blockquote>
<p>
Detlef's wording put in Proposed resolution.  Original wording here:
</p>
<blockquote>
<p>
Change 32.7.4 <a href="https://wg21.link/thread.condition.condvar">[thread.condition.condvar]</a> p12, p19 and
32.7.5 <a href="https://wg21.link/thread.condition.condvarany">[thread.condition.condvarany]</a> p10, p16:
</p>

<blockquote><p>
<i>Throws:</i> <ins>May throw</ins> <code>std::system_error</code> 
<ins>
if a precondition is not met.
</ins>
<del>when the effects or postcondition
cannot be achieved.</del>
</p></blockquote>
</blockquote>
</blockquote>

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


<blockquote><p>
Leave open, Detlef to provide improved wording.
</p></blockquote>

<p><i>[
2009-11-18 Anthony adds:
]</i></p>


<blockquote>
<p>
<code>condition_variable::wait</code> takes a <code>unique_lock&lt;mutex&gt;</code>. We
know whether or not a <code>unique_lock</code> owns a lock, through use of its
<code>owns_lock()</code> member.
</p>

<p>
I would like to propose the following resolution:
</p>

<blockquote>
<p>
Modify the first sentence of 32.7.4 <a href="https://wg21.link/thread.condition.condvar">[thread.condition.condvar]</a> p9:
</p>

<blockquote><pre>
void wait(unique_lock&lt;mutex&gt;&amp; lock);
</pre>
<blockquote><p>
9 <i>Precondition:</i> <del><code>lock</code> is locked by the calling thread</del>
<ins><code>lock.owns_lock()</code> is <code>true</code></ins>, and either
</p><p>...</p>
</blockquote>
</blockquote>

<p>
Replace 32.7.4 <a href="https://wg21.link/thread.condition.condvar">[thread.condition.condvar]</a> p11-13 with:
</p>

<blockquote><pre>
void wait(unique_lock&lt;mutex&gt;&amp; lock);
</pre>
<blockquote>
<p>...</p>
<p>
11 <i>Postcondition:</i> <del><code>lock</code> is locked by the calling
thread</del> <ins><code>lock.owns_lock()</code> is <code>true</code></ins>.
</p>

<p>
12 <i>Throws:</i> <code>std::system_error</code> <del>when the effects or
postcondition cannot be achieved</del> <ins>if the implementation detects that
the preconditions are not met or the effects cannot be achieved. Any exception
thrown by <code>lock.lock()</code> or <code>lock.unlock()</code></ins>.
</p>

<p>
13 <i>Error Conditions:</i> <ins>The error conditions are implementation
defined.</ins>
</p>

<ul>
<li><del>
equivalent error condition from <code>lock.lock()</code> or <code>lock.unlock()</code>.
</del></li>
</ul>

</blockquote>
</blockquote>
</blockquote>
</blockquote>

<p><i>[
2010 Pittsburgh:
]</i></p>


<blockquote>
<p>
There are heavy conflicts with adopted papers.
</p>
<p>
This issue is dependent on LWG <a href="lwg-defects.html#1268" title="The Mutex requirements in 30.4.1 and 30.4.2 are wrong (Status: Resolved)">1268</a><sup><a href="https://cplusplus.github.io/LWG/issue1268" title="Latest snapshot">(i)</a></sup>.
</p>
<p>
Leave open pending outstanding edits to the working draft. Detlef will provide
wording.
</p>
<p>
Possibly related to <a href="lwg-defects.html#964" title="Various threading bugs #14 (Status: Resolved)">964</a><sup><a href="https://cplusplus.github.io/LWG/issue964" title="Latest snapshot">(i)</a></sup>.
</p>

</blockquote>

<p><i>[2011-03-24 Madrid]</i></p>

<p><b>Rationale:</b></p>
<p>
This has been resolved since filing, with the introduction of <code>system_error</code> to the thread specification.
</p>




<p id="res-966"><b>Proposed resolution:</b></p>
<p>
Replace 32.7.4 <a href="https://wg21.link/thread.condition.condvar">[thread.condition.condvar]</a> p12, p19 and
32.7.5 <a href="https://wg21.link/thread.condition.condvarany">[thread.condition.condvarany]</a> p10, p16:
</p>

<blockquote>
<p><del>
<i>Throws:</i> <code>std::system_error</code> when the effects or
postcondition cannot be achieved.
</del></p>
<p><del>
Error conditions:
</del></p>
<ul>
<li><del>
equivalent error condition from <code>lock.lock()</code> or <code>lock.unlock()</code>.
</del></li>
</ul>

<p><ins>
<i>Throws:</i> It is implementation-defined whether a <code>std::system_error</code>
with implementation-defined error condition is thrown if the
precondition is not met.
</ins></p>
</blockquote>






</body>
</html>
