#include <pthread.h> int pthread_key_delete(pthread_key_t key);Service Program Name: QP0WPTHR
The pthread_key_delete() function deletes a process-wide thread local storage key. The pthread_key_delete() function does not run any destructors for the values associated with key in any threads. After a key is deleted, it may be returned by a subsequent call to pthread_key_create().
An attempt to delete a key that is out of range or not valid fails with EINVAL. An attempt to delete a valid key that has already been deleted or has not been returned from pthread_key_create() fails with ENOENT.
None.
If pthread_key_delete() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.
A destructor function is not called as a result of the application calling pthread_key_delete().
The value specified for the argument is not correct.
An entry for the key is not currently allocated.
See Code disclaimer information for information pertaining to code examples.
#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include "check.h"
pthread_key_t tlsKey = 0;
void globalDestructor(void *value)
{
printf("In global data destructor\n");
free(value);
pthread_setspecific(tlsKey, NULL);
}
int main(int argc, char **argv)
{
int rc=0;
int i=0;
printf("Enter Testcase - %s\n", argv[0]);
printf("Create a thread local storage key\n");
rc = pthread_key_create(&tlsKey, globalDestructor);
checkResults("pthread_key_create()\n", rc);
/* The key can now be used from all threads */
printf("Delete a thread local storage key\n");
rc = pthread_key_delete(tlsKey);
checkResults("pthread_key_delete()\n", rc);
printf("- The key should not be used from any thread\n");
printf("- after destruction.\n");
/* The key and any remaining values are now gone. */
printf("Main completed\n");
return 0;
}
Output:
Enter Testcase - QP0WTEST/TPKEYD0 Create a thread local storage key Delete a thread local storage key - The key should not be used from any thread - after destruction. Main completed
| Top | Pthread APIs | APIs by category |