summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/thread-local.html
blob: 079bf8b019ba4e635d96e137f7148f044eed64e5 (plain)
1
2
3
4
5
6
7
8
9
<div class="section-level-extent" id="Thread-Local"> <div class="nav-panel"> <p> Next: <a href="binary-constants" accesskey="n" rel="next">Binary Constants using the ‘<samp class="samp">0b</samp>’ Prefix</a>, Previous: <a href="unnamed-fields" accesskey="p" rel="prev">Unnamed Structure and Union Fields</a>, Up: <a href="c-extensions" accesskey="u" rel="up">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div>  <h1 class="section" id="Thread-Local-Storage"><span>6.64 Thread-Local Storage<a class="copiable-link" href="#Thread-Local-Storage"> ¶</a></span></h1>    <p>Thread-local storage (<abbr class="acronym">TLS</abbr>) is a mechanism by which variables are allocated such that there is one instance of the variable per extant thread. The runtime model GCC uses to implement this originates in the IA-64 processor-specific ABI, but has since been migrated to other processors as well. It requires significant support from the linker (<code class="command">ld</code>), dynamic linker (<code class="command">ld.so</code>), and system libraries (<samp class="file">libc.so</samp> and <samp class="file">libpthread.so</samp>), so it is not available everywhere. </p> <p>At the user level, the extension is visible with a new storage class keyword: <code class="code">__thread</code>. For example: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">__thread int i;
extern __thread struct state s;
static __thread char *p;</pre>
</div> <p>The <code class="code">__thread</code> specifier may be used alone, with the <code class="code">extern</code> or <code class="code">static</code> specifiers, but with no other storage class specifier. When used with <code class="code">extern</code> or <code class="code">static</code>, <code class="code">__thread</code> must appear immediately after the other storage class specifier. </p> <p>The <code class="code">__thread</code> specifier may be applied to any global, file-scoped static, function-scoped static, or static data member of a class. It may not be applied to block-scoped automatic or non-static data member. </p> <p>When the address-of operator is applied to a thread-local variable, it is evaluated at run time and returns the address of the current thread’s instance of that variable. An address so obtained may be used by any thread. When a thread terminates, any pointers to thread-local variables in that thread become invalid. </p> <p>No static initialization may refer to the address of a thread-local variable. </p> <p>In C++, if an initializer is present for a thread-local variable, it must be a <var class="var">constant-expression</var>, as defined in 5.19.2 of the ANSI/ISO C++ standard. </p> <p>See <a class="uref" href="https://www.akkadia.org/drepper/tls.pdf">ELF Handling For Thread-Local Storage</a> for a detailed explanation of the four thread-local storage addressing models, and how the runtime is expected to function. </p> <ul class="mini-toc"> <li><a href="c99-thread-local-edits" accesskey="1">ISO/IEC 9899:1999 Edits for Thread-Local Storage</a></li> <li><a href="c_002b_002b98-thread-local-edits" accesskey="2">ISO/IEC 14882:1998 Edits for Thread-Local Storage</a></li> </ul> </div>  <div class="nav-panel"> <p> Next: <a href="binary-constants">Binary Constants using the ‘<samp class="samp">0b</samp>’ Prefix</a>, Previous: <a href="unnamed-fields">Unnamed Structure and Union Fields</a>, Up: <a href="c-extensions">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
  <p class="_attribution-p">
    &copy; Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
    <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Thread-Local.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Thread-Local.html</a>
  </p>
</div>