<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2379: Obtaining native handle of the current thread</title>
<meta property="og:title" content="Issue 2379: Obtaining native handle of the current thread">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2379.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="2379"><a href="lwg-closed.html#2379">2379</a>. Obtaining native handle of the current thread</h3>
<p><b>Section:</b> 32.2.3 <a href="https://wg21.link/thread.req.native">[thread.req.native]</a>, 32.4.5 <a href="https://wg21.link/thread.thread.this">[thread.thread.this]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Matt Austern <b>Opened:</b> 2014-03-31 <b>Last modified:</b> 2015-10-21</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Class <code>thread</code> contains an implementation-defined type <code>thread::native_handle_type</code>, and an implementation-defined 
function <code>thread::native_handle()</code> that returns a value of that type. The presence and semantics of those members is 
implementation-defined; the intention is that they can be used for interoperability with libraries that rely on operating system 
specific features. (Posix libraries that accept arguments of type <code>pthread_t</code>, for example.)
<p/>
Unfortunately, there appears to be no native handle support for the equivalent of <code>pthread_self()</code>. We can use 
<code>this_thread::get_id()</code> to obtain the <code>thread::id</code> of the current thread, but there is no mechanism for converting 
a <code>thread::id</code> to a <code>thread::native_handle</code>.
<p/>
<em>Proposed wording:</em>
<p/>
In 32.4.5 <a href="https://wg21.link/thread.thread.this">[thread.thread.this]</a> add:
</p>
<blockquote><pre>
thread::native_handle_type native_handle(); <i>// See 30.2.3</i>
</pre></blockquote>
<p>
 to the <code>this_thread</code> namespace synopsis.
<p/>
<em>Rationale:</em>
<p/>
Informally, we could address this issue either by adding a new function in <code>this_thread</code> or by providing a mechanism for converting 
between <code>thread::id</code> and <code>thread::native_handle</code>. I propose the former because it seems more localized, and doesn't involve 
saying anything more about implementation defined native functionality than we currently do.
<p/>
It's intentional that the proposed resolution adds a declaration of <code>native_handle()</code> without adding a paragraph explaining what 
it does. This is because everything about <code>native_handle()</code> is implementation-defined. The standard does the same thing in 
32.4.3.6 <a href="https://wg21.link/thread.thread.member">[thread.thread.member]</a>. 
</p>

<p><i>[2015-02 Cologne]</i></p>

<p>
Handed over to SG1.
</p>

<p><i>[2015-05 Lenexa, SG1 response]</i></p>

<p>
Strong "don't care" reaction from SG1, after pointing out that this only matters in non-portable code, which can call pthread_self() or the like anyway, but the change also doesn't add any non-trivial implementation requirements.  This defused initial strong opinions on both sides.  Since this is essentially a feature request, we did not have sufficient consensus to proceed at this point.  There was a feeling we should reconsider after making more sense out of the much more general TLS issues we have been discussing.
</p>



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

<ol>
<li><p>Change 32.2.3 <a href="https://wg21.link/thread.req.native">[thread.req.native]</a> p1 as indicated:</p>

<blockquote>
<p>
-1- <del>Several classes described in this Clause have members</del><ins>This Clause includes several members named</ins> 
<code>native_handle_type</code> and <code>native_handle</code>. The presence of these members and their semantics is 
implementation-defined. [&hellip;]
</p>
</blockquote>
</li>

<li><p>In 32.4 <a href="https://wg21.link/thread.threads">[thread.threads]</a>, header <code>&lt;thread&gt;</code> synopsis, add:</p>

<blockquote>
<pre>
namespace std {
  [&hellip;]
  namespace this_thread {
    thread::id get_id() noexcept;
    <ins>thread::native_handle_type native_handle();</ins>
    [&hellip;]
  }
}
</pre>
</blockquote>
</li>

<li><p>In 32.4.5 <a href="https://wg21.link/thread.thread.this">[thread.thread.this]</a> add:</p>
<blockquote>
<pre>
namespace std {
  namespace this_thread {
    thread::id get_id() noexcept;
    <ins>thread::native_handle_type native_handle(); <i>// See 32.2.3 <a href="https://wg21.link/thread.req.native">[thread.req.native]</a></i></ins>
    [&hellip;]
  }
}
</pre>
</blockquote>
</li>
</ol>






</body>
</html>
