Storing Thread-specific Data with Thread Local Storage (Windows CE 5.0)
Thread local storage (TLS) is the method by which each thread in a multithreaded process allocates a location to store thread-specific data in.
There are several situations when you might want a thread to access unique data. For example, you might want to include a spreadsheet application that creates an 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 TlsAlloc function, 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.
After a slot is assigned, each thread can access its unique data by calling the TlsSetValue function to store data in the TLS slot, or the TlsGetValue function 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. |
See Also
Send Feedback on this topic to the authors