8.10.1 Thread Qualifier
The __thread qualifier indicates to the compiler that the qualified object must
be stored locally for each thread executed by the program. The compiler's Thread-Local
Storage (TLS) feature ensures that __thread-qualified objects are
accessed without data corruption or unexpected behavior across multiple threads, which
are known as thread-safe objects.
Thread-safe objects can be initialized when they are defined, if required. They cannot
use the persistent or address attributes, but they
can, however, be placed in a user-defined section using the section
attribute. Objects with automatic storage duration are inherently thread safe, so using
the __thread qualifier with them makes no sense, but objects with
permanent storage duration can be made thread-safe. Use of the
space(data) attribute with the __thread qualifier
is valid but has no effect; use of the space(prog) attribute and this
qualifier will result in an error.
__thread int result;
__thread __attribute__((section("my_section"))) int var = 7;If the TLS is not enabled, the __thread qualifier is ignored and any
object using it is linked and accessed like an ordinary C object.
