<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2140: Meaning of notify_all_at_thread_exit synchronization requirement?</title>
<meta property="og:title" content="Issue 2140: Meaning of notify_all_at_thread_exit synchronization requirement?">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2140.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="2140"><a href="lwg-defects.html#2140">2140</a>. Meaning of <code>notify_all_at_thread_exit</code> synchronization requirement?</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#C++14">C++14</a>
 <b>Submitter:</b> Pete Becker <b>Opened:</b> 2012-03-06 <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#C++14">C++14</a> status.</p>
<p><b>Discussion:</b></p>

<p>
<code>notify_all_at_thread_exit</code> has the following synchronization requirement:
</p>
<blockquote><p>
<i>Synchronization</i>: The call to <code>notify_all_at_thread_exit</code> and the completion of the destructors 
for all the current thread's variables of thread storage duration <em>synchronize with</em> (6.10.2 <a href="https://wg21.link/intro.multithread">[intro.multithread]</a>) 
calls to functions waiting on <code>cond</code>.
</p></blockquote>
<p>
The functions waiting on <code>cond</code> have already been called, otherwise they wouldn't be waiting. So how can a subsequent 
call to <code>notify_all_at_thread_exit</code> synchronize with them?
<p/>
Also, "synchronizes with" is a relationship between library calls (6.10.2 <a href="https://wg21.link/intro.multithread">[intro.multithread]</a>&#47;8), so it's not 
meaningful for completion of destructors for non-library objects. Presumably the intention wasn't so make library 
destructors special here.
</p>

<p><i>[2012-03-09 Jeffrey Yasskin comments:]</i></p>


<p>
I think the text should say that "<code>notify_all_at_thread_exit</code> and destructor calls are sequenced before
the <code>lk.unlock()</code>", and leave it at that, unless there's a funny implementation I haven't thought of.
</p>

<p><i>[2012-03-19 Hans Boehm comments:]</i></p>


<p>
I think the synchronization clause should just be replaced with (modulo wording tweaks):
<p/>
"The implied <code>lk.unlock()</code> call is sequenced after the destruction of all objects with thread storage duration 
associated with the current thread."
<p/>
as Jeffrey suggested.
<p/>
To use this correctly, the notifying thread has to essentially acquire the lock, set a variable indicating it's done, 
call <code>notify_all_at_thread_exit()</code>, while the waiting thread acquires the lock, and repeatedly waits on the 
cv until the variable is set, and then releases the lock.  That ensures that we have the proper synchronizes with 
relationship as a result of the lock.
</p>

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

<p>
The <code>lk.unlock()</code> refers back to the wording the previous paragraph.
</p>
<p>
Moved to review
</p>

<p><i>[2013-04-20, Bristol]</i></p>

<p>Accepted for the working paper</p>



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

<ol><li><p>Modify 32.7 <a href="https://wg21.link/thread.condition">[thread.condition]</a> p8 as indicated:</p>

<blockquote>
<pre>
void notify_all_at_thread_exit(condition_variable&amp; cond, unique_lock&lt;mutex&gt; lk);
</pre><blockquote>
<p>
[&hellip;]
<p/>
-8- <i>Synchronization</i>: <del>The call to <code>notify_all_at_thread_exit</code> and the completion of the destructors for
all the current thread's variables of thread storage duration synchronize with (6.10.2 <a href="https://wg21.link/intro.multithread">[intro.multithread]</a>) 
calls to functions waiting on <code>cond</code></del> <ins>The implied <code>lk.unlock()</code> call is sequenced after the 
destruction of all objects with thread storage duration associated with the current thread</ins>.
</p>
</blockquote></blockquote>
</li>
</ol>






</body>
</html>
