Thread-local storage
Encyclopedia
Thread-local storage is a computer programming method that uses static
Static memory allocation
Static memory allocation refers to the process of allocating memory at compile-time before the associated program is executed, unlike dynamic memory allocation or automatic memory allocation where memory is allocated as required at run-time....

 or global memory
Computer storage
Computer data storage, often called storage or memory, refers to computer components and recording media that retain digital data. Data storage is one of the core functions and fundamental components of computers....

 local to a thread
Thread (computer science)
In computer science, a thread of execution is the smallest unit of processing that can be scheduled by an operating system. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process...

.

This is sometimes needed because normally all threads in a process
Process (computing)
In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system , a process may be made up of multiple threads of execution that execute instructions concurrently.A computer program is a...

 share the same address space
Address space
In computing, an address space defines a range of discrete addresses, each of which may correspond to a network host, peripheral device, disk sector, a memory cell or other logical or physical entity.- Overview :...

, which is sometimes undesirable. In other words, data in a static or global variable
Global variable
In computer programming, a global variable is a variable that is accessible in every scope . Interaction mechanisms with global variables are called global environment mechanisms...

 is normally always located at the same memory location, when referred to by threads from the same process.
Variables
Variable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...

 on the stack
Call stack
In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, control stack, run-time stack, or machine stack, and is often shortened to just "the stack"...

 however are local to threads, because each thread has its own stack, residing in a different memory location.

Sometimes it is desirable that two threads referring to the same static or global variable are actually referring to different memory locations, thereby making the variable thread local, a canonical example being the C error code variable errno.

If it is possible to make at least a memory address
Memory address
A digital computer's memory, more specifically main memory, consists of many memory locations, each having a memory address, a number, analogous to a street address, at which computer programs store and retrieve, machine code or data. Most application programs do not directly read and write to...

 sized variable thread local, it is in principle possible to make arbitrarily sized memory blocks thread local, by allocating such a memory block and storing the memory address
Memory address
A digital computer's memory, more specifically main memory, consists of many memory locations, each having a memory address, a number, analogous to a street address, at which computer programs store and retrieve, machine code or data. Most application programs do not directly read and write to...

 of that block in a thread local variable.

Windows implementation

The API function
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

 TlsAlloc can be used to obtain an unused TLS slot index; the TLS slot index will then be considered ‘used’.

The TlsGetValue and TlsSetValue functions can then be used to read and write a memory address to a thread local variable identified by the TLS slot index. TlsSetValue can only affect the variable for the current thread.

The TlsFree function can be called to release the TLS slot index; the index will then be considered ‘unused’ and a new call to TlsAlloc can return it again.

Pthreads implementation

TLS with POSIX threads (Thread-Specific Data in Pthreads nomenclature) is similar to TlsAlloc and related functionality for Windows. pthread_key_create creates a key, with an optional destructor, that can later be associated with thread specific data via pthread_setspecific. The data can be retrieved using pthread_getspecific. If the thread specific value is not NULL, the destructor will be called when the thread exits. Additionally, key must be destroyed with pthread_key_delete.

Language-specific implementation

Apart from relying on programmers to call the appropriate API functions, it is also possible to extend the programming language to support TLS.

Object Pascal

In Delphi or Free Pascal
Free Pascal
Free Pascal Compiler is a free Pascal and Object Pascal compiler.In addition to its own Object Pascal dialect, Free Pascal supports, to varying degrees, the dialects of several other compilers, including those of Turbo Pascal, Delphi, and some historical Macintosh compilers...

 one can use the 'threadvar' reserved keyword instead of 'var' to declare variables using the thread-local storage.

var
mydata_process: integer;
threadvar
mydata_threadlocal: integer;

Java

In Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 thread local variables are implemented by the class
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...

 object. ThreadLocal holds variable of type T, which is accessible via get/set methods. For example ThreadLocal variable holding Integer value looks like this:

private static ThreadLocal myThreadLocalInteger = new ThreadLocal;

C++

C++11 introduces the thread_local keyword which can be used in the following cases
  • Namespace level (global) variables
  • File static variables
  • Function static variables
  • Static member variables


Aside from that, various C++ compiler implementations provide specific ways to declare thread-local variables:
  • Sun Studio C/C++
    Sun Studio (software)
    The Oracle Solaris Studio compiler suite is Oracle's flagship software development product for Solaris and Linux. It was formerly known as Sun Studio...

    , IBM XL C/C++, GNU C
    GNU Compiler Collection
    The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain...

     and Intel C/C++ (Linux systems) use the syntax:
    __thread int number;
  • Visual C++, Intel C/C++ (Windows systems), Borland C++ Builder
    C++ Builder
    C++Builder is a rapid application development environment, developed by Borland and owned by Embarcadero Technologies, for writing programs in the C++ programming language. C++Builder combines the Visual Component Library and IDE written in Delphi with a C++ compiler...

     and Digital Mars C++ use the syntax:
    __declspec(thread) int number;
  • Borland C++ Builder also supports the syntax:
    int __thread number;


On Windows versions prior to Vista and Server 2008, __declspec(thread) works in DLLs only when those DLLs are bound to the executable, and will not work for those loaded with LoadLibrary (a protection fault or data corruption may occur).

D

In the D programming language version 2, all static and global variables are thread-local by default and are declared with syntax similar to "normal" global and static variables in other languages. Regular global variables must be explicitly requested using the __gshared keyword:


int threadLocal; // This is a thread local variable.
__gshared int global; // This is a plain old global variable.

C# and other .NET languages

Static fields can be marked with the
ThreadStatic attribute:


class FooBar {
[ThreadStatic] static int foo;
}


In .NET 4.0 the System.Threading.ThreadLocal class is available for allocating and lazily loading thread local variables.

Also an API
is available for dynamically allocating thread local variables.

Common Lisp (and maybe other dialects)

Common Lisp
Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...

 provides a feature called dynamically scoped variables.

Dynamic variables have a binding which is private to the invocation of a function and all of the children called by that function.

This abstraction naturally maps to thread-specific storage, and Lisp implementations that provide threads do this. Common Lisp has numerous standard dynamic variables, and so threads cannot be sensibly added to an implementation of the language without these variables having thread-local semantics in dynamic binding.

For instance the standard variable *print-base* determines the default radix in which integers are printed. If this variable is overridden, then all enclosing code will print integers in an alternate radix:

;;; function foo and its children will print
in hexadecimal

(let ((*print-base* 16)) (foo))

If functions can execute concurrently on different threads, this binding has be properly thread local, otherwise thread will fight over who controls a global printing radix.

Python

In Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

 version 2.4 or later local class in threading module can be used to create thread-local storage.

import threading
mydata = threading.local
mydata.x = 1

Ruby

In Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

 thread local variables can be created/accessed using []=/[] methods.

Thread.current[:user_id] = 1

Perl

In Perl threads were added late in the evolution of the language, after a large body of existing code was already present on the Comprehensive Perl Archive Network. As a result, threads in Perl by default take their own local storage for all variables, to minimise the impact of threads on existing non-thread-aware code. In Perl, a thread-shared variable can be created using an attribute:

use threads;
use threads::shared;

my $localvar;
my $sharedvar :shared;

Underlying implementation in MS Windows

The above discussion indicates what interface a programmer uses to obtain thread-local storage, but not how this works behind the scenes. The underlying problem is that, since all threads share an address space, no fixed memory location can be used to store the location of the storage. The following discussion applies to Microsoft Windows-based systems, but similar models may be applicable to other systems.

In Windows, the thread-local storage is access via a table. (Actually, two tables, but they appear as one.) TlsAlloc returns an index to this table, unique per address space, for each call. Each thread has its own copy of the thread-local storage table. Hence, each thread can independently use TlsSetValue(index) and obtain the same value via TlsGetValue(index), because these set and look up an entry in the thread's own table. Only a single pointer is stored; any Windows system which offers more than one pointer of storage is either allocating multiple values or, more likely, obtaining storage from heap or stack and storing that in the pointer.

This leaves the question of how a per-thread table is to be found. In Windows, there is a Thread Information Block
Win32 Thread Information Block
In computing, the Win32 Thread Information Block is a data structure in Win32 on x86 that stores info about the currently running thread. This structure is also known as the Thread Environment Block ....

 for each thread. One of the entries in this block is the thread-local storage table for that thread. On x86 systems, the address of the Thread Information Block is stored in the FS register. In this way, access to thread-local storage carries a minimal overhead.

External links

  • ELF Handling For Thread-Local Storage — Document about an implementation in C
    C (programming language)
    C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

     or C++
    C++
    C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

    .
  • ACE_TSS< TYPE > Class Template Reference
  • RWTThreadLocal Class Template Documentation
  • Article "Use Thread Local Storage to Pass Thread Specific Data" by Doug Doedens
  • "Thread-Local Storage" by Lawrence Crowl
  • "Developer's Reference"
  • Article "It's Not Always Nice To Share" by Walter Bright
    Walter Bright
    Walter Bright is a computer programmer known for being the designer of the D programming language. He was also the main developer of the first C++ compiler that translated directly to object without going via C, Zortech C++ . Before the C++ compiler, he developed the Datalight C compiler, also...

  • Practical ThreadLocal usage in Java: http://blogs.captechconsulting.com/blog/balaji-muthuvarathan/persistence-pattern-using-threadlocal-and-ejb-interceptors
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK