Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.
TLS(Thread Specific Data or Thread Local Storage) is memory area that is specific to that particular thread.Any kind of DLL or exe can use this.
TLS is provided in chunks of 4 bytes. Each such 4 bytes of chunk is called a key. If a program needs TLS, then it has to create a key. Once a key is created, an ID (handle) is returned. And this ID should be used to read and write to the key.
The following APIs which are part of libpthread describes briefly how to create,read,write and free a key.
Creating a key
The pthread_key_create() API creates a new key and returns the ID.
Reading from a key
The pthread_getspecific() API can be used to read the value of the key. Here, it takes the ID of the key as an argument.
Writing to a key
pthread_setspecific() API can be used to set the value to a key. It takes the ID of the key as argument.
Freeing the key
The pthread_key_delete() API deletes the key in all the threads. Once the key is deleted, no thread should use the old handle/ID to set and get the key value.
If a thread is created after the key is created, then the key is available in the new thread from the beginning.