Use pthread_key_delete(3C) to destroy an existing thread-specific data key. Any memory associated with the key can be freed because the key has been invalidated. Reference to an invalid key returns an error.
int pthread_key_delete(pthread_key_t key);
#include <pthread.h> pthread_key_t key; int ret; /* key previously created */ ret = pthread_key_delete(key);
If a key has been deleted, any reference to the key with the pthread_setspecific() or pthread_getspecific() call yields undefined results.
The programmer must free any thread-specific resources before calling the pthread_key_delete() function. This function does not invoke any of the destructors. Repeated calls to pthread_key_create() and pthread_key_delete() can cause a problem.
The problem occurs because, in the Solaris implementation, a key value is never reused after pthread_key_delete() marks it as invalid. Every pthread_key_create() allocates a new key value and allocates more internal memory to hold the key information. An infinite loop of pthread_key_create() ... pthread_key_delete() will eventually exhaust all memory. If possible, call pthread_key_create() only once for each desired key and never call pthread_key_delete().
pthread_key_delete() returns zero after completing successfully. Any other return value indicates that an error occurred. When the following condition occurs, pthread_key_delete() fails and returns the corresponding value.
The key value is invalid.