Performance Tools for Software Developers - Thread-local variables and initialization

Thread Local Storage (TLS) variable is declared using storage class keyword __thread . The variables are created by the implementation such that there is one instance of the variable per thread. It requires support from linker (ld), dynamic linker (ld.so) and system libraries (libc.so and libpthread.so).

For example you can declare the TLS variables as below :

 __thread int i;
 extern __thread struct state s;
 static __thread char *p;

The __thread specifier may be used alone, with the extern or static specifiers, but with no other storage class specifier. The __thread 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.

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.

The compiler now issues an error when attempting to dynamically initialize a thread-local variable that is not a local static variable. For example:

int f();

__thread int i = f(); // now an error.

Furthermore, the address of a thread-local variable is no longer considered to be a constant. For example:

__thread int i = 2;
__thread int *p = &i; // now an error.

The __thread keyword is supported by Intel C++ compiler only if the underlying Gnu compiler version is Gnu 3.4 or higher.


Example :
// main.cpp
int f();

__thread int i = 0;
__thread int i2 = 2;
class foo {
public:
int static __thread i4;
};
int main()
{
int static __thread i3=0;
foo object_foo;
__thread int *p = &i2;
__thread int ii = f();

object_foo.i4 = 5;
return 0;
}


$ icc main.cpp
main.cpp(14): error #698: the modifier "thread" is not allowed on this declaration
__thread int *p = &i2;
^

main.cpp(15): error #698: the modifier "thread" is not allowed on this declaration
__thread int ii = f();
^

compilation aborted for main.cpp (code 2)

Operating System:

Red Hat* Linux, Red Hat* Desktop Linux* 3, Red Hat* Enterprise Linux Desktop 4, Red Hat* Desktop 3 Update 4, Red Hat* Enterprise Linux Desktop 3 Update 3, Red Hat* Enterprise Linux Desktop 3 Update 4, Red Hat* Enter prise Linux Desktop 3 Update 5, Red Hat* Enterprise Linux Desktop 4 Update 1, Red Hat* Enterprise Linux 2.1, SUSE* Linux 9.1, SUSE* Linux Enterprise Server 8.0, SUSE* Linux Enterprise Server 9.0, Red Hat* Enterprise Linux 4.0, Redhat* Desktop 3 Update 5, Redhat* Desktop 3 Update 6, Redhat* Desktop 3 Update 7, Redhat* Desktop 4 Update 2, Redhat* Desktop 4 Update 3, Redhat* Desktop 4 Update 4, SuSE* Linux* Enterprise* Desktop 10, SUSE* Linux Enterprise Server 10, Red Hat* Linux 6.2, Red Hat* Linux 6.2 SBE2, Red Hat* Linux 7.0, Red Hat* Linux 7.1, Red Hat* Linux 7.2, Red Hat* Linux 7.3, SUSE* Linux 7.3, SUSE* Linux 8.0, SUSE* Linux 8.1, Red Hat* Linux 8.0, SUSE* Linux 7.2, SUSE* Linux 7.1, SUSE* Linux 7.0, SUSE* Linux, Red Hat* Linux Advanced Server 2.x, Red Hat* Linux 9.0, Red Hat* Enterprise Linux 3.0, SUSE* Linux* 8.2, Red Hat* Linux Advanced Server 3.x, SUSE* Linux* 9.x
Per informazioni più dettagliate sulle ottimizzazioni basate su compilatore, vedere il nostro Avviso sull'ottimizzazione.