int pthread_once (pthread_once_t *once_control, Void (*init_routine) (void))
This function uses the Once_control variable with the initial value of Pthread_once_init to ensure that the Init_routine () function executes only once in the execution sequence of this process.
#include <pthread.h>
int Pthread_key_create (pthread_key_t *key, Void (*destructor) (void*)); Create thread-private data
int Pthread_key_delete (pthread_key_t key); Unregister thread Private data
Description
The function pthread_key_create () is used to create thread-private data. The function assigns an entry from the TSD pool and assigns its address value to key for later access. The 2nd parameter is an destroy function, which is optional and can be NULL when NULL, then the system calls the default destroy function to log off the related data. If it is not, the thread exits (called the Pthread_exit () function) when the data associated with the key lock is called as a parameter to release the allocated buffer, or to close the file stream.
Regardless of which thread called pthread_key_create (), the key created is accessible to all threads, but each thread can fill in the key with different values according to its own needs, equivalent to providing a global variable with the same name and different values ( This global variable is relative to the thread that owns the variable).
Unregister a TSD using the Pthread_key_delete () function. The function does not check whether a thread is currently using the TSD, nor does it call a cleanup function (destructor functions), but simply frees the TSD for use by the next call to Pthread_key_create (). In Linuxthread, it also sets the thread data item associated with it to NULL.
void *pthread_getspecific (pthread_key_t key); Read thread private data
int pthread_setspecific (pthread_key_t key, const void *value), write thread private data
The function pthread_setspecific () relates the value of pointer (not what the lock refers to) to key.
The function pthread_getspecific () reads the data associated with the key. The data types that are returned are void *, so you can point to any type of data.
Pthread_once,pthread_key_create,pthread_key_delete,pthread_getspecific,pthread_setspecific ()