<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1485: Unclear thread::id specification</title>
<meta property="og:title" content="Issue 1485: Unclear thread::id specification">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1485.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#NAD">NAD</a> status.</em></p>
<h3 id="1485"><a href="lwg-closed.html#1485">1485</a>. Unclear <code>thread::id</code> specification</h3>
<p><b>Section:</b> 32.4.3.2 <a href="https://wg21.link/thread.thread.id">[thread.thread.id]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> INCITS <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.thread.id">issues</a> in [thread.thread.id].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p><b>Addresses US-184</b></p>
<p>
It is unclear when a <code>thread::id</code> ceases to be meaningful.
The sentence "The library may reuse the value of a
<code>thread::id</code> of a terminated thread that can no longer be
joined." implies that some terminated threads can be
joined. It says nothing about detached threads.
</p>
<p><i>[
Resolution proposed by ballot comment:
]</i></p>

<blockquote><p>
Require a unique <code>thread::id</code> for every thread that is
(1) detached and not terminated or (2) has an associated <code>std::thread</code> 
object.
</p></blockquote>

<p><i>[
2010-11-22 Howard Hinnant observes
]</i></p>


<p>
A thread can either be running or terminated. Additionally a thread can be joined, detached, or neither.  
These combine into the five possible states shown in this table:
</p>

<table border="1">
<tr>
<th></th><th>Running</th><th>Terminated</th>
</tr>
<tr>
<th>Neither joined nor detached</th><td>shall not reuse id</td><td>shall not reuse id</td>
</tr>
<tr>
<th>detached</th><td>shall not reuse id</td><td>may reuse id</td>
</tr>
<tr>
<th>joined</th><td>impossible state</td><td>may reuse id</td>
</tr>
</table>
<p>
Only if a thread is neither joined nor detached can it be joined. Or said differently, if a 
thread has already been joined or detached, then it can not be joined. The sentence:
</p>
<blockquote><p>
The library may reuse the value of a <code>thread::id</code> of a terminated thread that can no longer be joined.
</p></blockquote>
<p>
precisely defines the two states shown in the above table where a thread::id may be reused.
</p>
<p>
The following program illustrates all of the possibilities:
</p>
<blockquote><pre>
#include &lt;mutex>
#include &lt;thread>
#include &lt;iostream>
#include &lt;chrono>

std::mutex mut;

void f()
{
   std::lock_guard&lt;std::mutex&gt; _(mut);
   std::cout &lt;&lt; "f id = " &lt;&lt; std::this_thread::get_id() &lt;&lt; " terminating\n";
}

void g()
{
   std::lock_guard&lt;std::mutex&gt; _(mut);
   std::cout &lt;&lt; "g id = " &lt;&lt; std::this_thread::get_id() &lt;&lt; " terminating\n";
}

int main()
{
   std::cout &lt;&lt; "main id = " &lt;&lt; std::this_thread::get_id() &lt;&lt; "\n";
   std::thread t1(f);
   std::thread(g).detach();
   std::this_thread::sleep_for(std::chrono::seconds(1));
   std::cout &lt;&lt; "g's thread::id can be reused here because g has terminated and is detached.\n";
   std::cout &lt;&lt; "f's thread::id can't be reused here because f has terminated but is still joinable.\n";
   std::cout &lt;&lt; "f id = " &lt;&lt; t1.get_id() &lt;&lt; "\n";
   t1.join();
   std::cout &lt;&lt; "f's thread::id can be reused here because f has terminated and is joined.\n";
   std::cout &lt;&lt; "f id = " &lt;&lt; t1.get_id() &lt;&lt; "\n";
}

main id = 0x7fff71197ca0
f id = 0x100381000 terminating
g id = 0x100581000 terminating
g's thread::id can be reused here because g has terminated and is detached.
f's thread::id can't be reused here because f has terminated but is still joinable.
f id = 0x100381000
f's thread::id can be reused here because f has terminated and is joined.
f id = 0x0
</pre></blockquote>

<p><i>[2011-02-11 Reflector discussion]</i></p>

<p>
Moved to Tentatively NAD after 5 votes.
</p>




<p id="res-1485"><b>Proposed resolution:</b></p>





</body>
</html>
