Linux multithreading practice (4) specific data of threads

Source: Internet
Author: User

Linux multithreading practice (4) specific data of threads

In a single-threaded program, we often use "global variables" to share data among multiple functions. However, in a multi-threaded environment, because the data space is shared, therefore, global variables are shared by all threads. However, sometimes it is necessary to provide a global variable private to the thread in the application design, which is valid only in a thread, but can be accessed across multiple functions. POSIX Thread library solves this problem by maintaining a certain data structure, which is called (Thread-specific-data or TSD ).

The related functions are as follows:

int pthread_key_create(pthread_key_t *key, void (*destr_function) (void *));  int pthread_key_delete(pthread_key_t key);    int pthread_setspecific(pthread_key_t key, const void *pointer);  void * pthread_getspecific(pthread_key_t key);    pthread_once_t once_control = PTHREAD_ONCE_INIT;  int pthread_once(pthread_once_t *once_control, void (*init_routine) (void));  

 

It can be seen that when pthread_key_create is called, a key value of thread-specific data (TSD) visible to all threads will be generated (for example, all threads will get a value of pkey [1 ), however, the key points to different real data. Although they are all pkey [1], they do not point to the same memory, but to their own actual data. Therefore, if Thread 0 changes the data pointed to by pkey [1], it cannot be imaged to thread n;

After the thread calls pthread_setspecific, it binds the specific data of each thread to thread_key_t. Although there is only one pthread_key_t, the specific data of each thread is independent of the memory space, the destructor function is executed when the thread exits.

/** Example 1: Use pthread_once to initialize the key only once. Note: Put the initialization of the key into init_routine **/pthread_key_t key; pthread_once_t once_control = PTHREAD_ONCE_INIT; typedef struct Tsd {pthread_t tid; char * str;} tsd_t; // The thread-specific data destruction function, // used to destroy the actual data void destructor_function (void * value) pointed to by each thread) {free (value); cout <"destructor... "<endl;} // initialization function, which puts key initialization into this function. // This ensures that the inti_routine function runs only once void init_routine () {pthread_key_create (& key, destructor_function); cout <"init... "<endl;} void * thread_routine (void * args) {pthread_once (& once_control, init_routine); // sets the thread-specific data tsd_t * value = (tsd_t *) malloc (sizeof (tsd_t); value-> tid = pthread_self (); value-> str = (char *) args; pthread_setspecific (key, value ); printf ("% s setspecific, address: % p \ n", (char *) args, value); // obtain the thread-specific data value = (tsd_t *) pthread_getspecific (key); printf ("tid: 0x % x, str = % s \ n", (unsigned int) value-> tid, value-> str ); sleep (2); // obtain the thread-specific data value again = (tsd_t *) pthread_getspecific (key); printf ("tid: 0x % x, str = % s \ n ", (unsigned int) value-> tid, value-> str); pthread_exit (NULL);} int main () {pthread_t tid1, tid2; pthread_create (& tid1, NULL, thread_routine, (void *) "thread1"); pthread_create (& tid2, NULL, thread_routine, (void *) "thread2"); pthread_join (tid1, NULL); pthread_join (tid2, NULL); pthread_key_delete (key); return 0 ;}
The running result is as follows:

Init ....
Thread1 setspecific, address: 0x7fe7a00008c0
Tid: 0xa8192700, str = thread1
Thread2 setspecific, address: 0x7fe7980008c0
Tid: 0xa7991700, str = thread2
Tid: 0xa8192700, str = thread1
Tid: 0xa7001700, str = thread2
Destructor...
Destructor...
 

The main thread creates two threads and then joins them to wait for them to exit. The execution function for each thread is thread_routine, and thread_routine calls pthread_once, this function indicates that if the first thread calls it, it will execute once_routine and then return it from once_routine, that is, pthread_once. Other subsequent threads will not execute once_routine when calling it, this is to call pthread_key_create only once, that is, generate a pthread_key_t value.

In the thread_routine function, the Data Type of a specific thread is customized. For different threads, the TSD content is different. Assume that thread 1 enters sleep after printing for the first time, thread 2 also starts to execute and call TSD and key_t of pthread_setspecific bound to thread 2. At this time, thread 1 calls pthread_getspecific and returns the TSD pointer bound to key_t, which is still the TSD pointer of thread 1, that is, although there is only one key_t, each thread has its own TSD.

Specific data; with 128 items, implemented through key-value, a thread creates a key, and other threads also create, but not to the same fast memory, they point to their own data,

This is thread-specific data.

In the above Code, even if it is Sleeep (2), the data of thread 1 is not affected by the data of thread 2, because it is private to the thread.

When the thread exits, it will be destroyed twice because two threads are created.

Here, tid is the thread id, str is the parameter passed to thread_routine. We can see that there are two different ptr, and destroy is called twice.

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.