Description |
 |
Beginning with the HP-UX 10.30 operating system
release, the __thread keyword defines
a thread-specific data variable, distinguishing it from other data
items that are shared by all threads. With a thread-specific data
variable, each thread has its own copy of the data item. These variables
eliminate the need to allocate thread-specific data dynamically, thus
improving performance.
This keyword is implemented as an HP specific
type qualifier, with the same syntax as type qualifiers const and volatile, but not the same semantics.
Syntax examples:
__thread int var;
int __thread var;
Semantics: Only variables of static duration can
be thread-specific. Thread-specific data objects can not be initialized.
Pointers of static duration that are not thread-specific may not be
initialized with the address of a thread-specific object assignment
is allowed. All global variables, thread-specific or not, are initialized
to zero by the linker implicitly.
Only one declaration, for example,
__thread int x;
is allowed in one compilation unit that contributes
to the program (including libraries linked into the executable). All
other declarations must be strictly references:
extern __thread int x;
Any other redeclarations of this thread-specific x will result in a duplicate definition error at
link time.
Even though __thread has the same syntax as a
type qualifier, it does not qualify the type, but is a storage class
specification for the data object. As such, it is type compatible
with non-thread-specific data objects of the same type. That is, a
thread-specific data int is type
compatible with an ordinary int,
(unlike const and volatile qualified int).