Thread private Data (TSD) "Reprint"

Source: Internet
Author: User

Source: http://www.cnblogs.com/yuxingfirst/archive/2012/07/25/2608612.html

Thread-specific thread storage in threads, thread specific Data. What is the use of thread storage? What does he mean by that? As you know, in a multithreaded program, all threads share variables in the program. Now there is a global variable that all threads can use to change its value. And if each thread wants to have it alone, then it needs to use thread storage. On the surface it looks like this is a global variable that all threads can use, and its value is stored separately in each thread. This is the meaning of thread storage.

Here is the specific usage of a thread store.

L Create a variable of type pthread_key_t type.

L call Pthread_key_create () to create the variable. The function has two parameters, the first parameter is the pthread_key_t variable declared above, and the second parameter is a cleanup function, which is called when the thread is freed by threads to be stored. The function pointer can be set to NULL, so the system will invoke the default cleanup function.

Pthread_setspcific () can be called when special values need to be stored in the thread. The function has two parameters, the first is the pthread_key_t variable declared earlier, and the second is the void* variable, so you can store any type of value.

L Call Pthread_getspecific () if you need to remove the stored value. The function's argument is the pthread_key_t variable mentioned earlier, which returns a value of type void *.

The following is a prototype of the previously mentioned function:

int pthread_setspecific (pthread_key_t key, const void *value);

void *pthread_getspecific (pthread_key_t key);

int Pthread_key_create (pthread_key_t *key, Void (*destructor) (void*));

Here is an example of how to use thread storage:

#include <stdio.h>#include <pthread.h>pthread_key_t key;void Echomsg (IntT) {printf ("destructor excuted in thread%d,param=%d\n", pthread_self (), t);}void * CHILD1 (void *ARG) {int tid=Pthread_self (); printf"Thread%d enter\n", Tid); Pthread_setspecific (Key, (void *) tid); Sleep2); printf"Thread%d returns%d\n", Tid,pthread_getspecific (key)); Sleep5);}void * CHILD2 (void *ARG) {int tid=Pthread_self (); printf"Thread%d enter\n", Tid); Pthread_setspecific (Key, (void *) tid); Sleep1); printf"Thread%d returns%d\n", Tid,pthread_getspecific (key)); Sleep5);}int main (voidint Tid1,tid2; printf ( "hello\n" Span style= "color: #000000;" >); Pthread_key_create (&key,echomsg); Pthread_create (& tid1,null,child1,null); Pthread_create (&tid2,null,child2,null); Sleep ( 10); Pthread_key_delete (key); printf (main thread exit\n  "); return 0    

The nature of the thread:

In fact, in Linux, the new thread is not in the original process, but the system calls clone () through a system. The system copied a process that is exactly the same as the original process and executes the thread function in the process. However, this copy process is not the same as fork. After the copy process and the original process share all the variables, the operating environment. In this way, the variable changes in the original process can be reflected in the copy process.

Thread private Data (TSD) "Reprint"

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.