Thread-local storage
Thread local storage ( TLS ) is memory space for each thread of a multi-barreled process may store thread-specific data. Variables with the TLS attribute are automatically assigned a separate instance for each thread. The compiler also automatically generates code that allows access to the exact copy of the TLS assigned to the thread. TLS can be used to protect data from unintentional changes by other threads, or to prevent race conditions .
Under Windows / IA-32 and Windows x86 or x64 e.g. B. access is via the segment register FS (x86) or GS (x64), which refers to the so-called thread environment block for each thread.
Code example for gcc using the __thread attribute :
__thread int i;
extern __thread struct state s;
static __thread char *p;
Under Microsoft-C using the __declspec (thread) attribute :
__declspec(thread) int i;
extern __declspec(thread) struct state s;
static __declspec(thread) char *p;
TLS is available for the IA-32 , x86-64 , SPARC , SuperH , Alpha and S390 architectures .
Web links
Individual evidence
- ↑ Visual Studio 2013: Local Thread Storage (TLS)
- ↑ a b Ulrich Drepper: ELF Handling For Thread-Local Storage Version 0.21 (PDF)