Pthread_once,pthread_key_create,pthread_key_delete,pthread_getspecific,pthread_setspecific ()

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.