Thread-local storage

from Wikipedia, the free encyclopedia
QS IT
This article was due to content flaws on the quality assurance side of the computer science editorial added. This is done in order to bring the quality of the articles from the subject area of ​​computer science to an acceptable level. Help to eliminate the shortcomings in this article and take part in the discussion !  ( + )

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

  1. Visual Studio 2013: Local Thread Storage (TLS)
  2. a b Ulrich Drepper: ELF Handling For Thread-Local Storage Version 0.21 (PDF)