POSIX multithreading--thread private data

Source: Internet
Author: User
Tags mutex posix terminates

1. When multiple threads share a variable, define the variable as a static or external variable, using a mutex to ensure secure access to the shared variable.
If each thread requires a private variable value, the value becomes the private data of the thread. The program creates a key that each thread independently sets or gets its own key value, and the private data between the threads does not affect each other.

2. Building thread-Private data
int Pthread_key_create (pthread_key_t *key,void (*destructor) (void *));
int Pthread_key_delete (pthread_key_t key);
int pthread_setspecific (pthread_key_t key,void *value);
void *pthread_getspecific (pthread_key_t key);

Private Data key, if created more than once, will overwrite the previously created key, the corresponding key value will be lost forever.

When you use Pthread_key_delete to release a private data key, you must ensure that all threads are not holding the value of the key.
A better practice is not to release the thread private Data key, because the thread private data key up to 128, very few programs need so many trees, there is no need to manually release.

The code examples are as follows:
Create data keys, set key values, get key values

#include <stdio.h>#include<pthread.h>typedefstructtsd_tag{pthread_t tid; Char*string;} tsd_t;pthread_key_t tsd_key;pthread_once_t key_once=Pthread_once_init;voidOne_routine (void) {pthread_key_create (&tsd_key,null);;}void*thread_routine (void*Arg) {Pthread_once (&key_once,one_routine); tsd_t*value; Value= (tsd_t*)malloc(sizeof(tsd_t));        Pthread_setspecific (Tsd_key,value); Value->tid =pthread_self (); Value-string= (Char*) Arg; Value= (tsd_t*) pthread_getspecific (Tsd_key); Sleep (2); Value= (tsd_t*) pthread_getspecific (Tsd_key); printf ("%s done...\n",value->string);}intMainvoid) {pthread_t tid1,tid2; Pthread_create (&tid1,null,thread_routine,"Thread 1"); Pthread_create (&tid2,null,thread_routine,"Thread 2");         Pthread_exit (NULL);} 
View Code

3.destructor function
The destructor function is called only when the thread terminates. When the thread terminates, pthreads the thread private data to NULL before calling the key's destructor function, so if the thread private data value is the address of the heap store and wants to be disposed in the destructor function, the arguments passed to the destructor must be used. Rather than the pthread_getspecific parameter. You can customize the destructor function.

A key value of NULL means that the thread should no longer have a value for the key value, rather than a null value. Otherwise, if you subsequently call Pthread_key_create to establish the key, the thread will receive the old value.

The destructor function of the thread private Data key is not called when the key value is replaced. That is, if a pointer to a struct is used as a key value in the heap, and a new structure is assigned to the same data key, a pointer to the old structure does not call the destructor function.

The code examples are as follows:

Three-thread private variable data, custom destructor function, after thread end, free heap storage, after all threads end, destroy private Data key.

#include <stdio.h>#include<pthread.h>typedefstructprivate_tag{pthread_t tid; Char*string;} private_t;pthread_key_t key;pthread_mutex_t Mutex=Pthread_mutex_initializer;LongCounter =0;voidKey_destructor (void*value) {private_t*Private= (private_t*) value; printf ("thread \ "%s\" exiting...\n",Private-string);  Free(value); Pthread_mutex_lock (&mutex); Counter--; if(counter<=0) {pthread_key_delete (key); printf ("Key deleted...\n"); } pthread_mutex_unlock (&mutex);}void*key_get (void){        void*value; Value=pthread_getspecific (key); if(value==NULL) {printf ("malloc\n"); Value=malloc(sizeof(private_t)); Pthread_setspecific (Key, (void*) value); }        returnvalue;}void*thread_routine (void*Arg) {private_t*value; Value= (private_t*) Key_get (); Value->tid =pthread_self (); Value-string= (Char*) Arg; printf ("thread \ "%s\" starting...\n",value->string); Sleep (2); returnNULL;}intMainvoid) {pthread_t tid1,tid2; private_t*value; Pthread_key_create (&key,key_destructor); Counter=3; Value= (private_t *) Key_get (); Value->tid =pthread_self (); Value-string="Main Thread"; Pthread_create (&tid1,null,thread_routine,"Thread 1"); Pthread_create (&tid2,null,thread_routine,"Thread 2"); printf ("exiting\n");                        Pthread_exit (NULL);} 
View Code

POSIX multithreading--thread private data

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.