Microsoft Windows CE 3.0  

Using Thread Local Storage

Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Thread local storage(TLS) is the method by which each thread in a multithreaded process allocates a location in which to store thread-specific data. There are several situations in which you may want a thread to access unique data. One example might include a spreadsheet application that creates a new instance of the same thread each time the user opens a new spreadsheet. The DLL that provides the functions for various spreadsheet operations can use TLS to save data about the current state of each spreadsheet.

TLS uses a TLS array to save thread-specific data. When a process is created, Windows CE allocates a 64-slot array for each running process. When a DLL attaches to a process, the DLL calls the TlsAllocfunction, which looks through the array to find a free slot. The function then marks the slot "in use" and returns an index value to the newly assigned slot. If no slots are available, the function returns –1. Individual threads cannot call TlsAlloc. Only a process or DLL can call the function and it must do so before creating the threads that will use the TLS slot.

Once a slot has been assigned, each thread can access its unique data by calling the TlsSetValuefunction to store data in the TLS slot, or the TlsGetValuefunction to retrieve data from the slot.

The following table describes the TLS functions that are supported by Windows CE.

Function Description
TlsAlloc Allocates a TLS index. The index is available to any thread in the process for storing and retrieving thread-specific values. You must store this index in global memory, where all threads can retrieve its value.
TlsFree Releases the TLS index, making it available for reuse.
TlsGetValue Retrieves the value that is pointed to by the TLS index.
TlsSetValue Stores a value in the slot that is pointed to by the TLS index.


 Last updated on Tuesday, May 18, 2004

© 2004 Microsoft Corporation. All rights reserved.