Chapter 6. Extensions to the C Language Family
6.50. Thread-Local Storage
Thread-local storage (TLS) 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 (
), so it is not available everywhere.
libpthread.so
At the user level, the extension is visible with a new storage class keyword:
__thread int i;
extern __thread struct state s;
static __thread char *p;
The
specifier may be used alone, with the
__thread
storage class specifier. When used with
the other storage class specifier.
The
specifier may be applied to any global, file-scoped static, function-scoped static, or
__thread
static data member of a class. It may not be applied to block-scoped automatic or non-static data
member.
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.
No static initialization may refer to the address of a thread-local variable.
In C++, if an initializer is present for a thread-local variable, it must be a
as defined in 5.19.2 of the ANSI/ISO C++ standard.
See http://people.redhat.com/drepper/tls.pdfELF Handling For Thread-Local Storage for a detailed
explanation of the four thread-local storage addressing models, and how the run-time is expected to
function.
6.50.1. ISO/IEC 9899:1999 Edits for Thread-Local Storage
The following are a set of changes to ISO/IEC 9899:1999 (aka C99) that document the exact semantics
of the language extension.
[5.1.2 Execution environments]
•
Add new text after paragraph 1
Within either execution environment, a thread is a flow of control within a program. It is implementation
defined whether or not there may be more than one thread associated with a program. It is implementation
defined how threads beyond the first are created, the name and type of the function called at thread startup,
and how threads may be terminated. However, objects with thread storage duration shall be initialized
before thread startup.
[6.2.4 Storage durations of objects]
•
Add new text before paragraph 3
An object whose identifier is declared with the storage-class specifier
duration. Its lifetime is the entire execution of the thread, and its stored value is initialized only once,
prior to thread startup.
[6.4.1 Keywords]
•
Add
.
__thread
[6.7.1 Storage-class specifiers]
•
), dynamic linker (
ld
extern
or
extern
static
), and system libraries (
ld.so
__thread
or
specifiers, but with no other
static
,
must appear immediately after
__thread
constant-expression
__thread
207
and
libc.so
. For example:
,
has thread storage
Need help?
Do you have a question about the ENTERPRISE LINUX 3 - USING GCC and is the answer not in the manual?
Questions and answers