Linux thread private data --- TSD pool, linux --- tsd

Source: Internet
Author: User

Linux thread private data --- TSD pool, linux --- tsd

All threads in the process share the data space of the process, so the global variables are shared by all threads. In some scenarios, a Thread needs to save its own private Data. In this case, you can create a Thread-specific Data TSD. Inside the thread, private data can be accessed by various interfaces of the thread, but it is blocked from other threads.

The thread private data adopts the one-key multi-value technology, and one key corresponds to multiple values. Data is accessed by key values.

When using the thread private data, you need to create an associated key for each thread. in linux, there are four main interfaces to achieve this:

1. pthread_key_create: Create a key.

int pthread_key_create(pthread_key_t *key, void (*destr_function) (void*));

First, allocate an item from the TSD pool of linux, and then assign the value to the key for future access. The first parameter of the interface is the pointer to the parameter, and the second parameter is the function pointer. if the pointer is not empty, when the thread exits after execution, the content that the key points to is the input parameter call destr_function () to release the allocated buffer zone and other data.

After the key is created, all threads can access it because it is a global variable. Each thread can enter different values in the key as needed, which is equivalent to providing a global variable with the same name and different values, that is, one-click multi-value. One-click multi-value depends on a struct array, that is

static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX] ={{0,NULL}};

Pthread_key_struct is defined:

struct pthread_key_struct{  /* Sequence numbers.  Even numbers indicated vacant entries.  Note     that zero is even.  We use uintptr_t to not require padding on     32- and 64-bit machines.  On 64-bit machines it helps to avoid     wrapping, too.  */  uintptr_t seq;  /* Destructor for the data.  */  void (*destr) (void *);};

The PTHREAD_KEYS_MAX value is 1024

Creating a TSD is equivalent to setting the seq value of an element in the struct array to "in_use", returning its index to * key, and setting destr_function () to destr (). When pthread_key_create creates a new thread private data key, the system searches for the key structure array of the process in which it is located, finds an unused element, and assigns its index to * key.

2. pthread_setspecific: sets the thread private data for the specified key value.

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

This interface associates the pointer value (instead of the content it points to) with the key. When pthread_setspecific is used to specify the new thread data for a key, the thread must release the original data to recycle space.

3. pthread_getspecific: Read the private data of the thread from the specified key.

void * pthread_getspecific(pthread_key_t key);

4. pthread_key_delete: delete a key.

void * pthread_getspecific(pthread_key_t key);

This interface is used to delete a key. The function is only to set the element of the key in the pthread_keys array of the struct to "un_use". The thread data associated with the key change will not be released, therefore, the release of the thread's private data must be prior to the deletion of the key.


How to use private global variables under VC Multithreading

It seems that there is no good way. Only one parameter is added to the function (a pointer or reference can be referenced if a variable is used). variables can be defined in the thread and passed in.

Synchronization between processes and threads in linux

The address space is shared between threads in the same process. Therefore, global variables are shared. Therefore, modifications in one thread are visible to other threads.

Thread private data includes stack, command, register, and thread private data.

Related Article

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.