The thread-specific data interfaces allow each thread to associate an arbitrary value with a shared key value created by the program.
Thread-specific data is like a global variable in which each thread can keep its own value, but is accessible to the thread anywhere in the program.
Use the following routines to create and access thread-specific data:
· The pthread_keycreate( ) routine to create a unique key value
· The pthread_setspecific( ) routine to associate data with a key
· The pthread_getspecific( ) routine to obtain the data associated with a key
The pthread_keycreate( ) routine generates a unique key value that is shared by all threads in the process. This key is the identifier of a piece of thread-specific data. Each thread uses the same key value to assign or retrieve a thread-specific value. This keeps your data separate from other thread-specific data. One call to the pthread_keycreate( ) routine creates a cell in all threads. Call this routine to specify a routine to be called to destroy the context value associated with this key when the thread terminates.
The pthread_setspecific( ) routine associates the address of some data with a specific key. Multiple threads associate different data (by specifying different addresses) with the same key. For example, each thread points to a different block of dynamically allocated memory that it has reserved.
The pthread_getspecific( ) routine obtains the address of the thread-specific data value associated with a specified key. Use this routine to locate the data associated with the current thread's context.