<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2135: Unclear requirement for exceptions thrown in condition_variable::wait()</title>
<meta property="og:title" content="Issue 2135: Unclear requirement for exceptions thrown in condition_variable::wait()">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2135.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++14">C++14</a> status.</em></p>
<h3 id="2135"><a href="lwg-defects.html#2135">2135</a>. Unclear requirement for exceptions thrown in <code>condition_variable::wait()</code></h3>
<p><b>Section:</b> 32.7.4 <a href="https://wg21.link/thread.condition.condvar">[thread.condition.condvar]</a>, 32.7.5 <a href="https://wg21.link/thread.condition.condvarany">[thread.condition.condvarany]</a> <b>Status:</b> <a href="lwg-active.html#C++14">C++14</a>
 <b>Submitter:</b> Pete Becker <b>Opened:</b> 2012-03-06 <b>Last modified:</b> 2015-10-03</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#C++14">C++14</a> status.</p>
<p><b>Discussion:</b></p>

<p>
<code>condition_varible::wait()</code> (and, presumably, <code>condition_variable_any::wait()</code>, although 
I haven't looked at it) says that it calls <code>lock.unlock()</code>, and if <code>condition_variable::wait()</code> 
exits by an exception it calls <code>lock.lock()</code> on the way out. But if the initial call to 
<code>lock.unlock()</code> threw an exception, does it make sense to call <code>lock.lock()</code>? We simply 
don't know the state of that lock object, and it's probably better not to touch it.
<p/>
That aside, once the <code>wait()</code> call has been unblocked, it calls <code>lock.lock()</code>. If <code>lock.lock()</code> 
throws an exception, what happens? The requirement is:
</p>
<blockquote><p>
If the function exits via an exception, <code>lock.lock()</code> shall be called prior to exiting the function scope.
</p></blockquote>
<p>
That can be read in two different ways. One way is as if it said "<code>lock.lock()</code> shall have been called &hellip;", 
i.e. the original, failed, call to <code>lock.lock()</code> is all that's required. But a more natural reading is 
that wait has to call <code>lock.lock()</code> again, even though it already failed.
<p/>
I think this wording suffers from being too general. There are two possible exception sources: the initial call 
to <code>lock.unlock()</code> and the final call to <code>lock.lock()</code>. Each one should have its own requirement. 
Lumping them together muddles things.
</p>

<p><i>[2012, Portland: move to Open]</i></p>

<p>
Pablo: <code>unlock</code> failing is easy -- the call leaves it locked.
       The second case, trying to <code>lock</code> fails -- what can you do?
       This is an odd state as we <em>had</em> it locked before was called wait.
       Maybe we should call <code>terminate</code> as we cannot meet the post-conditions.
       We could throw a different exception.
</p>
<p>
Hans: calling <code>terminate</code> makes sense as we're likely to call it soon anyway
      and at least we have some context.
</p>
<p>
Detlef: what kind of locks might be being used?
</p>
<p>
Pablo: condition variables are 'our' locks so this is less of a problem.
       <code>condition_variable_any</code> might be more problematic.
</p>
<p>
The general direction is to call <code>terminate</code> if the lock cannot be reacquired.
</p>
<p>
Pablo: Can we change the wording to 'leaves the mutex locked' ?
</p>
<p>
Hans: so if the <code>unlock</code> throws we simply propagate the exception.
</p>
<p>
Move the issue to open and add some formal wording at a later time.
</p>

<p><i>[2013-09 Chicago: Resolved]</i></p>


<p>
Detlef improves wording. Daniel suggests to introduce a <i>Remarks</i> element for the special
"If the function fails to meet the postcondition..." wording and applies this to the proposed
wording.
</p>



<p id="res-2135"><b>Proposed resolution:</b></p>
<p>This wording is relative to N3691.</p>

<ol>
<li><p>Edit 32.7.4 <a href="https://wg21.link/thread.condition.condvar">[thread.condition.condvar]</a> as indicated:</p>

<blockquote><pre>
void wait(unique_lock&lt;mutex&gt;&amp; lock);
</pre><blockquote>
<p>
[&hellip;]
<p/>
-10- <i>Effects:</i>
</p>
<ul>
<li><p>Atomically calls <code>lock.unlock()</code> and blocks on <code>*this</code>.</p></li>
<li><p>When unblocked, calls <code>lock.lock()</code> (possibly blocking on the lock), then returns.</p></li>
<li><p>The function will unblock when signaled by a call to <code>notify_one()</code> or a call to <code>notify_all()</code>,
or spuriously.</p></li>
<li><p><del>If the function exits via an exception, <code>lock.lock()</code> shall be called prior to exiting the function
scope.</del></p></li>
</ul>
<p>
<ins>-?- <i>Remarks:</i> If the function fails to meet the postcondition, <code>std::terminate()</code> shall be called 
(14.6.2 <a href="https://wg21.link/except.terminate">[except.terminate]</a>). [<i>Note:</i> This can happen if the re-locking of the mutex throws an 
exception. &mdash; <i>end note</i>]</ins>
<p/>
-11- <i>Postcondition:</i> <code>lock.owns_lock()</code> is true and <code>lock.mutex()</code> is locked by the calling 
thread. 
<p/>
-12- <i>Throws:</i> <ins>Nothing</ins><del><code>system_error</code> when an exception is required (30.2.2)</del>.
<p/>
<del>-13- <i>Error conditions:</i></del>
</p>
<ul>
<li><p><del>equivalent error condition from <code>lock.lock()</code> or <code>lock.unlock()</code>.</del></p></li>
</ul>
</blockquote>

<blockquote>
<pre>
template &lt;class Predicate&gt;
void wait(unique_lock&lt;mutex&gt;&amp; lock, Predicate pred);
</pre><blockquote>
<p>
[&hellip;]
<p/>
<ins>-?- <i>Remarks:</i> If the function fails to meet the postcondition, <code>std::terminate()</code> shall be called 
(14.6.2 <a href="https://wg21.link/except.terminate">[except.terminate]</a>). [<i>Note:</i> This can happen if the re-locking of the mutex throws an 
exception. &mdash; <i>end note</i>]</ins>
<p/>
-16- <i>Postcondition:</i> <code>lock.owns_lock()</code> is true and <code>lock.mutex()</code> is locked by the calling 
thread.
<p/>
-17- <i>Throws:</i> <del><code>system_error</code> when an exception is required (30.2.2),</del> timeout-related exceptions 
(30.2.4)<del>,</del> or any exception thrown by <code>pred</code>.
<p/>
<del>-18- <i>Error conditions:</i></del>
</p>
<ul>
<li><p><del>equivalent error condition from <code>lock.lock()</code> or <code>lock.unlock()</code>.</del></p></li>
</ul>
</blockquote></blockquote>

<blockquote>
<pre>
template &lt;class Clock, class Duration&gt;
  cv_status wait_until(unique_lock&lt;mutex&gt;&amp; lock,
    const chrono::time_point&lt;Clock, Duration&gt;&amp; abs_time);
</pre><blockquote>
<p>
[&hellip;]
<p/>
-20- <i>Effects:</i> 
</p>
<ul>
<li><p>[&hellip;]</p></li>
<li><p>If the function exits via an exception, <code>lock.lock()</code> shall be called prior to exiting the function
<del>scope</del>.</p></li>
</ul>
<p>
<ins>-?- <i>Remarks:</i> If the function fails to meet the postcondition, <code>std::terminate()</code> shall be called 
(14.6.2 <a href="https://wg21.link/except.terminate">[except.terminate]</a>). [<i>Note:</i> This can happen if the re-locking of the mutex throws an 
exception. &mdash; <i>end note</i>]</ins>
<p/>
-21- <i>Postcondition:</i> <code>lock.owns_lock()</code> is true and <code>lock.mutex()</code> is locked by the calling 
thread.
<p/>
[&hellip;]
<p/>
-23- <i>Throws:</i> <del><code>system_error</code> when an exception is required (30.2.2) or</del> timeout-related 
exceptions (30.2.4).
<p/>
<del>-24- <i>Error conditions:</i></del>
</p>
<ul>
<li><p><del>equivalent error condition from <code>lock.lock()</code> or <code>lock.unlock()</code>.</del></p></li>
</ul>
</blockquote></blockquote>

<blockquote>
<pre>
template &lt;class Rep, class Period&gt;
  cv_status wait_for(unique_lock&lt;mutex&gt;&amp; lock,
    const chrono::duration&lt;Rep, Period&gt;&amp; rel_time);
</pre><blockquote>
<p>
[&hellip;]
<p/>
<ins>-?- <i>Remarks:</i> If the function fails to meet the postcondition, <code>std::terminate()</code> shall be called 
(14.6.2 <a href="https://wg21.link/except.terminate">[except.terminate]</a>). [<i>Note:</i> This can happen if the re-locking of the mutex throws an 
exception. &mdash; <i>end note</i>]</ins>
<p/>
-28- <i>Postcondition:</i> <code>lock.owns_lock()</code> is true and <code>lock.mutex()</code> is locked by the calling 
thread.
<p/>
[&hellip;]
<p/>
-29- <i>Throws:</i> <del><code>system_error</code> when an exception is required (30.2.2) or</del> timeout-related 
exceptions (30.2.4).
<p/>
<del>-30- <i>Error conditions:</i></del>
</p>
<ul>
<li><p><del>equivalent error condition from <code>lock.lock()</code> or <code>lock.unlock()</code>.</del></p></li>
</ul>
</blockquote></blockquote>

<blockquote>
<pre>
template &lt;class Clock, class Duration, class Predicate&gt;
  bool wait_until(unique_lock&lt;mutex&gt;&amp; lock,
    const chrono::time_point&lt;Clock, Duration&gt;&amp; abs_time,
	Predicate pred);
</pre><blockquote>
<p>
[&hellip;]
<p/>
<ins>-?- <i>Remarks:</i> If the function fails to meet the postcondition, <code>std::terminate()</code> shall be called 
(14.6.2 <a href="https://wg21.link/except.terminate">[except.terminate]</a>). [<i>Note:</i> This can happen if the re-locking of the mutex throws an 
exception. &mdash; <i>end note</i>]</ins>
<p/>
-33- <i>Postcondition:</i> <code>lock.owns_lock()</code> is true and <code>lock.mutex()</code> is locked by the calling 
thread.
<p/>
[&hellip;]
<p/>
-35- <i>Throws:</i> <del><code>system_error</code> when an exception is required (30.2.2),</del> timeout-related exceptions 
(30.2.4)<del>,</del> or any exception thrown by <code>pred</code>.
<p/>
<del>-36- <i>Error conditions:</i></del>
</p>
<ul>
<li><p><del>equivalent error condition from <code>lock.lock()</code> or <code>lock.unlock()</code>.</del></p></li>
</ul>
</blockquote></blockquote>

<blockquote>
<pre>
template &lt;class Rep, class Period, class Predicate&gt;
  bool wait_for(unique_lock&lt;mutex&gt;&amp; lock,
    const chrono::duration&lt;Rep, Period&gt;&amp; rel_time,
	Predicate pred);
</pre><blockquote>
<p>
[&hellip;]
<p/>
<ins>-?- <i>Remarks:</i> If the function fails to meet the postcondition, <code>std::terminate()</code> shall be called 
(14.6.2 <a href="https://wg21.link/except.terminate">[except.terminate]</a>). [<i>Note:</i> This can happen if the re-locking of the mutex throws an 
exception. &mdash; <i>end note</i>]</ins>
<p/>
-40- <i>Postcondition:</i> <code>lock.owns_lock()</code> is true and <code>lock.mutex()</code> is locked by the calling 
thread.
<p/>
[&hellip;]
<p/>
-42- <i>Throws:</i> <del><code>system_error</code> when an exception is required (30.2.2),</del> timeout-related exceptions 
(30.2.4)<del>,</del> or any exception thrown by <code>pred</code>.
<p/>
<del>-43- <i>Error conditions:</i></del>
</p>
<ul>
<li><p><del>equivalent error condition from <code>lock.lock()</code> or <code>lock.unlock()</code>.</del></p></li>
</ul>
</blockquote></blockquote>

</blockquote>

</li>

<li><p>Edit 32.7.5 <a href="https://wg21.link/thread.condition.condvarany">[thread.condition.condvarany]</a> as indicated:</p>

<blockquote><pre>
template&lt;class Lock&gt;
void wait(Lock&amp; lock);
</pre><blockquote>
<p>
[&hellip;]
<p/>
-10- <i>Effects:</i>
</p>
<ul>
<li><p>Atomically calls <code>lock.unlock()</code> and blocks on <code>*this</code>.</p></li>
<li><p>When unblocked, calls <code>lock.lock()</code> (possibly blocking on the lock) and returns.</p></li>
<li><p>The function will unblock when signaled by a call to <code>notify_one()</code>, a call to <code>notify_all()</code>,
or spuriously.</p></li>
<li><p><del>If the function exits via an exception, <code>lock.lock()</code> shall be called prior to exiting the function
scope.</del></p></li>
</ul>
<p>
<ins>-?- <i>Remarks:</i> If the function fails to meet the postcondition, <code>std::terminate()</code> shall be called 
(14.6.2 <a href="https://wg21.link/except.terminate">[except.terminate]</a>). [<i>Note:</i> This can happen if the re-locking of the mutex throws an 
exception. &mdash; <i>end note</i>]</ins>
<p/>
-11- <i>Postcondition:</i> <code>lock</code> is locked by the calling thread. 
<p/>
-12- <i>Throws:</i> <ins>Nothing</ins><del><code>system_error</code> when an exception is required (30.2.2)</del>.
<p/>
<del>-13- <i>Error conditions:</i></del>
</p>
<ul>
<li><p><del>equivalent error condition from <code>lock.lock()</code> or <code>lock.unlock()</code>.</del></p></li>
</ul>

</blockquote>

<blockquote>
<pre>
template &lt;class Lock, class Clock, class Duration&gt;
  cv_status wait_until(Lock&amp; lock, const chrono::time_point&lt;Clock, Duration&gt;&amp; abs_time);
</pre><blockquote>
<p>
[&hellip;]
<p/>
-15- <i>Effects:</i> 
</p>
<ul>
<li><p>[&hellip;]</p></li>
<li><p>If the function exits via an exception, <code>lock.lock()</code> shall be called prior to exiting the function
<del>scope</del>.</p></li>
</ul>
<p>
<ins>-?- <i>Remarks:</i> If the function fails to meet the postcondition, <code>std::terminate()</code> shall be called 
(14.6.2 <a href="https://wg21.link/except.terminate">[except.terminate]</a>). [<i>Note:</i> This can happen if the re-locking of the mutex throws an 
exception. &mdash; <i>end note</i>]</ins>
<p/>
-16- <i>Postcondition:</i> <code>lock</code> is locked by the calling thread.
<p/>
[&hellip;]
<p/>
-18- <i>Throws:</i> <del><code>system_error</code> when an exception is required (30.2.2) or</del> timeout-related 
exceptions (30.2.4).
<p/>
<del>-19- <i>Error conditions:</i></del>
</p>
<ul>
<li><p><del>equivalent error condition from <code>lock.lock()</code> or <code>lock.unlock()</code>.</del></p></li>
</ul>
</blockquote></blockquote>

<blockquote>
<pre>
template &lt;class Lock, class Rep, class Period&gt;
  cv_status wait_for(Lock&amp; lock, const chrono::duration&lt;Rep, Period&gt;&amp; rel_time);
</pre><blockquote>
<p>
[&hellip;]
<p/>
<ins>-?- <i>Remarks:</i> If the function fails to meet the postcondition, <code>std::terminate()</code> shall be called 
(14.6.2 <a href="https://wg21.link/except.terminate">[except.terminate]</a>). [<i>Note:</i> This can happen if the re-locking of the mutex throws an 
exception. &mdash; <i>end note</i>]</ins>
<p/>
-22- <i>Postcondition:</i> <code>lock</code> is locked by the calling thread.
<p/>
[&hellip;]
<p/>
-23- <i>Throws:</i> <del><code>system_error</code> when an exception is required (30.2.2) or</del> timeout-related 
exceptions (30.2.4).
<p/>
<del>-24- <i>Error conditions:</i></del>
</p>
<ul>
<li><p><del>equivalent error condition from <code>lock.lock()</code> or <code>lock.unlock()</code>.</del></p></li>
</ul>
</blockquote></blockquote>

</blockquote>

</li>
</ol>






</body>
</html>
