pthread_key_delete()--Delete Thread Local Storage Key
Syntax:
#include <pthread.h> int pthread_key_delete(pthread_key_t key);Service Program Name: QP0WPTHR
Default Public Authority: *USE
Threadsafe: Yes
Signal Safe: No
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.
Authorities and Locks
None.
Parameters
- key
- (Input) The thread local storage key returned from pthread_key_create()
Return Value
- 0
- pthread_key_delete() was successful.
- value
- pthread_key_delete() was not successful. value is set to indicate the error condition.
Error Conditions
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().
- [EINVAL]
The value specified for the argument is not correct.
- [ENOENT]
An entry for the key is not currently allocated.
Related Information
- The <pthread.h> header file. See Header files for Pthread functions.
- pthread_getspecific()--Get Thread Local Storage
Value by Key
- pthread_key_create()--Create Thread Local Storage
Key
- pthread_setspecific()--Set Thread Local Storage by Key
Example
Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
#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
API introduced: V4R3
[ Back to top | Pthread APIs | APIs by category ]