<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 857: condition_variable::time_wait return bool error prone</title>
<meta property="og:title" content="Issue 857: condition_variable::time_wait return bool error prone">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue857.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="857"><a href="lwg-defects.html#857">857</a>. <code>condition_variable::time_wait</code> return <code>bool</code> error prone</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#C++11">C++11</a>
 <b>Submitter:</b> Beman Dawes <b>Opened:</b> 2008-06-13 <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#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The meaning of the <code>bool</code> returned by <code>condition_variable::timed_wait</code> is so
obscure that even the class' designer can't deduce it correctly. Several
people have independently stumbled on this issue.
</p>
<p>
It might be simpler to change the return type to a scoped enum:
</p>
<blockquote><pre>
enum class timeout { not_reached, reached };
</pre></blockquote>

<p>
That's the same cost as returning a <code>bool</code>, but not subject to mistakes. Your example below would be:
</p>

<blockquote><pre>
if (cv.wait_until(lk, time_limit) == timeout::reached )
  throw time_out();
</pre></blockquote>

<p><i>[
Beman to supply exact wording.
]</i></p>


<p><i>[
San Francisco:
]</i></p>


<blockquote>
<p>
There is concern that the enumeration names are just as confusing, if
not more so, as the bool. You might have awoken because of a signal or a
spurious wakeup, for example.
</p>
<p>
Group feels that this is a defect that needs fixing.
</p>
<p>
Group prefers returning an enum over a void return.
</p>
<p>
Howard to provide wording.
</p>
</blockquote>

<p><i>[
2009-06-14 Beman provided wording.
]</i></p>


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


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



<p id="res-857"><b>Proposed resolution:</b></p>
<p>
Change Condition variables 32.7 <a href="https://wg21.link/thread.condition">[thread.condition]</a>, Header
condition_variable synopsis, as indicated:
</p>

<blockquote><pre>
namespace std {
  class condition_variable;
  class condition_variable_any;

  <ins>enum class cv_status { no_timeout, timeout };</ins>
}
</pre></blockquote>

<p>
Change class <code>condition_variable</code> 32.7.4 <a href="https://wg21.link/thread.condition.condvar">[thread.condition.condvar]</a> as indicated:
</p>

<blockquote><pre>
class condition_variable { 
public:
  ...
  template &lt;class Clock, class Duration&gt;
    <del>bool</del> <ins>cv_status</ins> wait_until(unique_lock&lt;mutex&gt;&amp; lock,
                    const chrono::time_point&lt;Clock, Duration&gt;&amp; abs_time);
  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);

  template &lt;class Rep, class Period&gt;
    <del>bool</del> <ins>cv_status</ins> wait_for(unique_lock&lt;mutex&gt;&amp; lock,
                  const chrono::duration&lt;Rep, Period&gt;&amp; rel_time);
  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);
  ...
};

...

template &lt;class Clock, class Duration&gt;
  <del>bool</del> <ins>cv_status</ins> wait_until(unique_lock&lt;mutex&gt;&amp; lock,
                  const chrono::time_point&lt;Clock, Duration&gt;&amp; abs_time);
</pre>
<blockquote>
<p>
-15- <i>Precondition:</i> <code>lock</code> is locked by the calling thread, and either
</p>
<ul>
<li>
no other thread is waiting on this <code>condition_variable</code> object or
</li>
<li>
<code>lock.mutex()</code> returns the same value for each of the <code>lock</code>
arguments supplied by all concurrently waiting threads (via <code>wait</code>,
<code>wait_for</code> or <code>wait_until</code>.).
</li>
</ul>

<p>
-16- <i>Effects:</i>
</p>

<ul>
<li>
Atomically calls <code>lock.unlock()</code> and blocks on <code>*this</code>.
</li>
<li>
When unblocked, calls <code>lock.lock()</code> (possibly blocking on the lock) and returns.
</li>
<li>
The function will unblock when signaled by a call to <code>notify_one()</code>,
a call to <code>notify_all()</code>, <del>by 
the current time exceeding <code>abs_time</code></del> <ins>if <code>Clock::now() &gt;= abs_time</code></ins>,
or spuriously.
</li>
<li>
If the function exits via an exception, <code>lock.unlock()</code> shall be called prior
to exiting the function scope.
</li>
</ul>

<p>
-17- <i>Postcondition:</i> <code>lock</code> is locked by the calling thread.
</p>

<p>
-18- <i>Returns:</i> <del><code>Clock::now() &lt; abs_time</code></del>
<ins><code>cv_status::timeout</code> if the function unblocked because <code>abs_time</code>
was reached, otherwise <code>cv_status::no_timeout</code></ins>.
</p>

<p>
-19- <i>Throws:</i> <code>std::system_error</code> when the effects or postcondition
cannot be achieved.
</p>

<p>
-20- <i>Error conditions:</i>
</p>

<ul>
<li>
<code>operation_not_permitted</code> &mdash; if the thread does not own the lock.
</li>
<li>
equivalent error condition from <code>lock.lock()</code> or <code>lock.unlock()</code>.
</li>
</ul>
</blockquote>

<pre>
template &lt;class Rep, class Period&gt;
  <del>bool</del> <ins>cv_status</ins> wait_for(unique_lock&lt;mutex&gt;&amp; lock,
                const chrono::duration&lt;Rep, Period&gt;&amp; rel_time);

</pre>
<blockquote>
<p>
-21- <i><del>Effects</del> <ins>Returns</ins>:</i>
</p>
<blockquote><pre>
wait_until(lock, chrono::monotonic_clock::now() + rel_time)
</pre></blockquote>
<p>
<del>-22- <i>Returns:</i> <code>false</code> if the call is returning because the time
duration specified by <code>rel_time</code> has elapsed, 
otherwise <code>true</code>.</del>
</p>

<p><i>[
This part of the wording may conflict with <a href="lwg-defects.html#859" title="Monotonic Clock is Conditionally Supported? (Status: C++11)">859</a><sup><a href="https://cplusplus.github.io/LWG/issue859" title="Latest snapshot">(i)</a></sup> in detail, but does
not do so in spirit.  If both issues are accepted, there is a logical merge.
]</i></p>

</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>
-23- <i>Effects:</i>
</p>
<blockquote><pre>
while (!pred()) 
  if (<del>!</del>wait_until(lock, abs_time) <ins>== cv_status::timeout</ins>) 
    return pred(); 
return true;
</pre></blockquote>

<p>
-24- <i>Returns:</i> <code>pred()</code>.
</p>

<p>
-25- [<i>Note:</i>
The returned value indicates whether the predicate evaluates to
<code>true</code> regardless of whether the timeout was triggered.
&mdash; <i>end note</i>].
</p>
</blockquote>
</blockquote>

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

<blockquote><pre>
class condition_variable_any {
public:
  ...
  template &lt;class Lock, class Clock, class Duration&gt;
    <del>bool</del> <ins>cv_status</ins> wait_until(Lock&amp; lock,
                    const chrono::time_point&lt;Clock, Duration&gt;&amp; abs_time);
  template &lt;class Lock, class Clock, class Duration, class Predicate&gt;
    bool wait_until(Lock&amp; lock,
                    const chrono::time_point&lt;Clock, Duration&gt;&amp; abs_time,
                    Predicate pred);

  template &lt;class Lock, class Rep, class Period&gt;
    <del>bool</del> <ins>cv_status</ins> wait_for(Lock&amp; lock,
                  const chrono::duration&lt;Rep, Period&gt;&amp; rel_time);
  template &lt;class Lock, class Rep, class Period, class Predicate&gt;
    bool wait_for(Lock&amp; lock,
                  const chrono::duration&lt;Rep, Period&gt;&amp; rel_time,
                  Predicate pred);
  ...
};

...

template &lt;class Lock, class Clock, class Duration&gt;
  <del>bool</del> <ins>cv_status</ins> wait_until(Lock&amp; lock,
                  const chrono::time_point&lt;Clock, Duration&gt;&amp; abs_time);
</pre>

<blockquote>

<p>
-13- <i>Effects:</i>
</p>

<ul>
<li>
Atomically calls <code>lock.unlock()</code> and blocks on <code>*this</code>.
</li>
<li>
When unblocked, calls <code>lock.lock()</code> (possibly blocking on the lock) and returns.
</li>
<li>
The function will unblock when signaled by a call to <code>notify_one()</code>,
a call to <code>notify_all()</code>, <del>by 
the current time exceeding <code>abs_time</code></del> <ins>if <code>Clock::now() &gt;= abs_time</code></ins>,
or spuriously.
</li>
<li>
If the function exits via an exception, <code>lock.unlock()</code> shall be called prior
to exiting the function scope.
</li>
</ul>

<p>
-14- <i>Postcondition:</i> <code>lock</code> is locked by the calling thread.
</p>

<p>
-15- <i>Returns:</i> <del><code>Clock::now() &lt; abs_time</code></del>
<ins><code>cv_status::timeout</code> if the function unblocked because <code>abs_time</code>
was reached, otherwise <code>cv_status::no_timeout</code></ins>.
</p>

<p>
-16- <i>Throws:</i> <code>std::system_error</code> when the effects or postcondition
cannot be achieved.
</p>

<p>
-17- <i>Error conditions:</i>
</p>

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

<pre>
template &lt;class Lock, class Rep, class Period&gt;
  <del>bool</del> <ins>cv_status</ins> wait_for(Lock&amp; lock,
                const chrono::duration&lt;Rep, Period&gt;&amp; rel_time);

</pre>

<blockquote>
<p>
-18- <i><del>Effects</del> <ins>Returns</ins>:</i>
</p>
<blockquote><pre>
wait_until(lock, chrono::monotonic_clock::now() + rel_time)
</pre></blockquote>

<p>
<del>-19- <i>Returns:</i> <code>false</code> if the call is returning because the time
duration specified by <code>rel_time</code> has elapsed, 
otherwise <code>true</code>.</del>
</p>

<p><i>[
This part of the wording may conflict with <a href="lwg-defects.html#859" title="Monotonic Clock is Conditionally Supported? (Status: C++11)">859</a><sup><a href="https://cplusplus.github.io/LWG/issue859" title="Latest snapshot">(i)</a></sup> in detail, but does
not do so in spirit.  If both issues are accepted, there is a logical merge.
]</i></p>


</blockquote>

<pre>
template &lt;class Lock, class Clock, class Duration, class Predicate&gt; 
  bool wait_until(Lock&amp; lock, 
                  const chrono::time_point&lt;Clock, Duration&gt;&amp; <del>rel_time</del> <ins>abs_time</ins>, 
                  Predicate pred);
</pre>

<blockquote>
<p>
-20- <i>Effects:</i>
</p>
<blockquote><pre>
while (!pred()) 
  if (<del>!</del>wait_until(lock, abs_time) <ins>== cv_status::timeout</ins>) 
    return pred(); 
return true;
</pre></blockquote>

<p>
-21- <i>Returns:</i> <code>pred()</code>.
</p>

<p>
-22- [<i>Note:</i>
The returned value indicates whether the predicate evaluates to
<code>true</code> regardless of whether the timeout was triggered.
&mdash; <i>end note</i>].
</p>
</blockquote>

</blockquote>






</body>
</html>
