Multithreaded Programming Guide

pthread_key_delete Syntax

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().