<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1498: Unclear specification for [thread.condition]</title>
<meta property="og:title" content="Issue 1498: Unclear specification for [thread.condition]">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1498.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="1498"><a href="lwg-defects.html#1498">1498</a>. Unclear specification for [thread.condition]</h3>
<p><b>Section:</b> 32.7 <a href="https://wg21.link/thread.condition">[thread.condition]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Switzerland <b>Opened:</b> 2010-08-25 <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.condition">issues</a> in [thread.condition].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p><b>Addresses CH-29</b></p>

<p>
It is unclear if a spurious wake-up during the loop and reentering
of the blocked state due to a repeated execution
of the loop will adjust the timer of the blocking with the
respect to the previously specified <code>rel_time</code> value.
</p>

<p><i>[
Resolution proposed by ballot comment:
]</i></p>


<blockquote><p>
Make it clear (e.g. by a note) that when reexecuting
the loop the waiting time when blocked
will be adjusted with respect to the elapsed time of
the previous loop executions.
</p></blockquote>

<p><i>[
2010-08-13 Peter Sommerlad comments and provides wording:
]</i></p>


<blockquote><p>
Problem: It is unclear if a spurious wake-up during the loop and re-entering of the blocked state due 
to a repeated execution of the loop will adjust the timer of the blocking with the respect to the 
previously specified <code>rel_time</code> value.
<p/>
Proposed Resolution from CH29:
<p/>
Make it clear (e.g. by a note) that when re-executing the loop the waiting time when blocked will be 
adjusted with respect to the elapsed time of the previous loop executions.
<p/>
Discussion in Rapperswil:
<p/>
Assuming the introduction of a mandatory <code>steady_clock</code> proposed by US-181 to the FCD the 
specification of <code>condition_variable::wait_for</code> can be defined in terms of <code>wait_until</code> 
using the <code>steady_clock</code>. This is also interleaving with US-181, because that touches the 
same paragraph (30.5.1 p 25, p34 and 30.5.2 p 20, p 28 in n3092.pdf)
<p/>
(The "as if" in the proposed solutions should be confirmed by the standardization terminology experts)
</p></blockquote>

<p><i>[
2010-11 Batavia: Resolved by applying <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3191.htm">n3191</a>
]</i></p>


<blockquote>
<ol>
<li>Change 32.7.4 <a href="https://wg21.link/thread.condition.condvar">[thread.condition.condvar]</a> paragraph 25, <code>wait_for</code> <i>Effects</i> as indicated:
<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>
<blockquote><p>
[..]
<p/>
25 <i>Effects</i>: <ins>as if</ins>
</p>
<blockquote><pre>
<ins>return wait_until(lock, chrono::steady_clock::now() + rel_time);</ins>
</pre></blockquote>
<ul>
<li><del>Atomically calls <code>lock.unlock()</code> and blocks on <code>*this</code>.</del>
</li>
<li><del>When unblocked, calls <code>lock.lock()</code> (possibly blocking on the lock), then returns.</del>
</li>
<li><del>The function will unblock when signaled by a call to <code>notify_one()</code> or a call to <code>notify_all()</code>,
by the elapsed time <code>rel_time</code> passing (30.2.4), or spuriously.</del>
</li>
<li><del>If the function exits via an exception, <code>lock.lock()</code> shall be called prior to exiting the function scope.</del>
</li>
</ul>
</blockquote>
</li>
<li>Change 32.7.4 <a href="https://wg21.link/thread.condition.condvar">[thread.condition.condvar]</a> paragraph 34, <code>wait_for</code> with predicate <i>Effects</i> as indicated:
<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>
<blockquote><p>
[..]
<p/>
34 <i>Effects</i>: <ins>as if</ins>
</p>
<blockquote><pre>
<ins>return wait_until(lock, chrono::steady_clock::now() + rel_time, std::move(pred));</ins>
</pre></blockquote>
<ul>
<li><del>Executes a loop: Within the loop the function first evaluates <code>pred()</code> and exits the loop if the
result is <code>true</code>.
</del></li>

<li><del>Atomically calls <code>lock.unlock()</code> and blocks on <code>*this</code>.
</del></li>

<li><del>When unblocked, calls <code>lock.lock()</code> (possibly blocking on the lock).
</del></li>

<li><del>The function will unblock when signaled by a call to <code>notify_one()</code> or a call to <code>notify_all()</code>,
by the elapsed time <code>rel_time</code> passing (30.2.4), or spuriously.
</del></li>

<li><del>If the function exits via an exception, <code>lock.lock()</code> shall be called prior to exiting the function
scope.
</del></li>

<li><del>The loop terminates when <code>pred()</code> returns <code>true</code> or when the time duration specified by <code>rel_time</code>
has elapsed.
</del></li>

</ul>
</blockquote>
</li>

<li>Change 32.7.5 <a href="https://wg21.link/thread.condition.condvarany">[thread.condition.condvarany]</a> paragraph 20, <code>wait_for</code> <i>Effects</i> as indicated:
<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>
<blockquote><p>
20 <i>Effects</i>: <ins>as if</ins>
</p><blockquote><pre>
<ins>return wait_until(lock, chrono::steady_clock::now() + rel_time);</ins>
</pre></blockquote>
<ul>
<li><del>Atomically calls <code>lock.unlock()</code> and blocks on <code>*this</code>.
</del></li>

<li><del>When unblocked, calls <code>lock.lock()</code> (possibly blocking on the lock), then returns.
</del></li>

<li><del>The function will unblock when signaled by a call to <code>notify_one()</code> or a call to <code>notify_all()</code>,
by the elapsed time <code>rel_time</code> passing (30.2.4), or spuriously.
</del></li>

<li><del>If the function exits via an exception, <code>lock.unlock()</code> shall be called prior to exiting the function
scope.
</del></li>
</ul>
</blockquote>
</li>

<li>Change 32.7.5 <a href="https://wg21.link/thread.condition.condvarany">[thread.condition.condvarany]</a> paragraph 28, <code>wait_for</code> with predicate <i>Effects</i> as indicated:
<blockquote><pre>
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);
</pre></blockquote>
<blockquote><p>
28 <i>Effects</i>: <ins>as if</ins>
</p><blockquote><pre>
<ins>return wait_until(lock, chrono::steady_clock::now() + rel_time, std::move(pred));</ins>
</pre></blockquote>
<ul>
<li><del>Executes a loop: Within the loop the function first evaluates <code>pred()</code> and exits the loop if the
result is <code>true</code>.
</del></li>

<li><del>Atomically calls <code>lock.unlock()</code> and blocks on <code>*this</code>.
</del></li>

<li><del>When unblocked, calls <code>lock.lock()</code> (possibly blocking on the lock).
</del></li>

<li><del>The function will unblock when signaled by a call to <code>notify_one()</code> or a call to <code>notify_all()</code>,
by the elapsed time <code>rel_time</code> passing (30.2.4), or spuriously.
</del></li>

<li><del>If the function exits via an exception, <code>lock.unlock()</code> shall be called prior to exiting the function
scope.
</del></li>

<li><del>The loop terminates when <code>pred()</code> returns <code>true</code> or when the time duration specified by <code>rel_time</code>
has elapsed.
</del></li>
</ul>
</blockquote>
</li>

</ol>

</blockquote>


<p id="res-1498"><b>Proposed resolution:</b></p><p>
Resolved by <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3191.htm">n3191</a>.
</p>




</body>
</html>
