📄 thread-local.html
字号:
<html lang="en">
<head>
<title>Using the GNU Compiler Collection (GCC)</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Using the GNU Compiler Collection (GCC)">
<meta name="generator" content="makeinfo 4.3">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">
<!--
Copyright © 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
<p>Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "GNU General Public License" and "Funding
Free Software", the Front-Cover texts being (a) (see below), and with
the Back-Cover Texts being (b) (see below). A copy of the license is
included in the section entitled "GNU Free Documentation License".
<p>(a) The FSF's Front-Cover Text is:
<p>A GNU Manual
<p>(b) The FSF's Back-Cover Text is:
<p>You have freedom to copy and modify this GNU Manual, like GNU
software. Copies published by the Free Software Foundation raise
funds for GNU development.-->
</head>
<body>
<div class="node">
<p>
Node:<a name="Thread-Local">Thread-Local</a>,
Previous:<a rel="previous" accesskey="p" href="Unnamed-Fields.html#Unnamed%20Fields">Unnamed Fields</a>,
Up:<a rel="up" accesskey="u" href="C-Extensions.html#C%20Extensions">C Extensions</a>
<hr><br>
</div>
<h3 class="section">Thread-Local Storage</h3>
<p>Thread-local storage (<small>TLS</small>) is a mechanism by which variables
are allocated such that there is one instance of the variable per extant
thread. The run-time 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>ld</code>), dynamic linker (<code>ld.so</code>), and
system libraries (<code>libc.so</code> and <code>libpthread.so</code>), so it
is not available everywhere.
<p>At the user level, the extension is visible with a new storage
class keyword: <code>__thread</code>. For example:
<pre class="example"> __thread int i;
extern __thread struct state s;
static __thread char *p;
</pre>
<p>The <code>__thread</code> specifier may be used alone, with the <code>extern</code>
or <code>static</code> specifiers, but with no other storage class specifier.
When used with <code>extern</code> or <code>static</code>, <code>__thread</code> must appear
immediately after the other storage class specifier.
<p>The <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>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>No static initialization may refer to the address of a thread-local variable.
<p>In C++, if an initializer is present for a thread-local variable, it must
be a <var>constant-expression</var>, as defined in 5.19.2 of the ANSI/ISO C++
standard.
<p>See <a href="http://people.redhat.com/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 run-time
is expected to function.
<ul class="menu">
<li><a accesskey="1" href="C99-Thread-Local-Edits.html#C99%20Thread-Local%20Edits">C99 Thread-Local Edits</a>:
<li><a accesskey="2" href="C--98-Thread-Local-Edits.html#C++98%20Thread-Local%20Edits">C++98 Thread-Local Edits</a>:
</ul>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -