Thread-local storage (TLS)

Threads share the data of the process to which it belongs to. This data sharing provides one of the benefits of multithreaded programming. However, in some circumstances, each thread might need its own copy of certain data. Such data is called thread-local storage (or TLS).

For example, in a transaction-processing system, we might service each transaction in a separate thread. Each transaction might be assigned a unique identifier. To associate each thread with its unique identifier, we could use thread-local storage.

It is easy to puzzle TLS with local variables. During a single function invocation only local variables are visible, whereas TLS data are visible across function invocations. In some ways, TLS is similar to static data. The only difference is that TLS data are unique to each thread. Most thread libraries-including Windows and Pthreads-provide some form of support for thread-local storage; Java provides support as well.