#include
<pthread.h>
int pthread_key_create(pthread_key_t *keyp, void (*destructor)(void *value));
int pthread_setspecific(pthread_key_t key, const void *value);
void *pthread_getspecific(pthread_key_t key);
int pthread_key_delete(pthread_key_t key);
#include <thread.h>
int thr_keycreate(thread_key_t *keyp, void (*destructor)(void *value));
int thr_setspecific(thread_key_t key, void *value);
int thr_getspecific(thread_key_t key, void **valuep);
Upon key creation, a new key is assigned the value NULL for all active threads. Additionally, upon thread creation, all previously created keys in the new thread are assigned the value NULL.
Optionally, a destructor function, destructor, may be associated with each key. Upon thread exit, if a key has a non-NULL destructor function and the thread has a non-NULL value associated with that key, the destructor function is called with the current associated value. If more than one destructor exists for a thread when it exits, the order of destructor calls is unspecified.
Proper synchronization of key storage and access must be ensured by the caller. The value argument to either pthread_setspecific() or thr_setspecific() is generally a pointer to a block of dynamically allocated memory reserved by the calling thread for its own use. (see "Examples" section below).
At thread exit, the destructor function, which is associated at time of creation, is called and it uses the specific key value as its sole argument.
Although pthread_key_create()’s or thr_keycreate()’s destructor function should clean-up the key’s thread-specific-data storage, pthread_key_delete() needs to be used to free the storage associated with the key.
Solaris threads do not have a similar delete function.
If the following conditions occur, pthread_key_create(), pthread_setspecific(), thr_keycreate(), or thr_setspecific() return the corresponding error number:
For each of the following conditions, if the condition is detected, pthread_setspecific(), thr_setspecific(), or pthread_key_delete() return the corresponding error number:
For each argument you pass to the executable of this example, a thread is created and privately bound to the string-value of that argument.
#define _REENTRANT#include <pthread.h>void *thread_specific_data(), free();#define MAX_ARGC 20pthread_t tid[MAX_ARGC];int num_threads;main( int argc, char *argv[] ) { int i; num_threads = argc - 1; for( i = 0; i < num_threads; i++) pthread_create(&tid[i], NULL, thread_specific_data, argv[i+1]); for( i = 0; i < num_threads; i++) pthread_join(tid[i], NULL);} /* end main */void *thread_specific_data(char private_data[]){
static pthread_mutex_t keylock; /* static ensures only one copy of keylock */ static pthread_key_t key; static int once_per_keyname = 0; void * tsd = NULL;
if (!once_per_keyname) { /* see pthread_once(3T) */ pthread_mutex_lock(&keylock); if (!once_per_keyname++) /* retest with lock */ pthread_key_create(&key, free); pthread_mutex_unlock(&keylock); } tsd = pthread_getspecific(key); if (tsd == NULL) { tsd = (void *)malloc(strlen(private_data) + 1); strcpy(tsd, private_data); pthread_setspecific(key, tsd); printf("tsd for %d = %s\n",thr_self(),(char *)pthread_getspecific(key)); sleep(2) ; printf("tsd for %d remains %s\n",thr_self(),(char *)pthread_getspecific(key)); }} /* end thread_specific_data */voidfree(void *v) { /* application-specific clean-up function */}