Each thread has an area in which thread-specific data information is kept.
You can associate arbitrary data with a thread's context. You can think of this as the ability to add user-specified fields to the current thread's context or as global variables that have private values in each thread. A thread-specific data key is shared by all threads within the process-each thread has its own unique value for that shared key.
Use the following routines to create and access thread-specific data information:
One call to the pthread_key_create routine creates a thread- specific data key shared by all threads. You can specify a destructor routine to destroy the context value associated with this key when any thread terminates. For example, to free heap storage the process must create each key exactly once- otherwise, subsequent creates will overwrite the first. See Section 2.4 for information about the one-time initialization in a threaded environment.
This routine associates some data with a specific key. Each thread can associate its own private data with the same key. For example, each thread might store a pointer to a block of dynamically allocated memory that it has reserved. Although each thread has its own block of memory, your code always uses the same key to get the current thread's block.
This routine obtains the current thread's thread-specific data value associated with a specified key.