Thread control-private data is transferred from Monalisa's _ blog

Source: Internet
Author: User

In a multi-threaded environment, all threads in a process share the data space of the process. Therefore, global variables are shared by all threads. In programming, you sometimes need to save the global variables of the thread. This special variable is only valid within a thread. For example, the common variable errno, which returns the standard error code. Errno should not be a local variable. Almost every function should be able to access it, but it cannot be used as a global variable, otherwise, the output in one thread may be an error message from another thread. This problem can be solved by creating the thread's private data (thread-specific data, or TSD. In a thread, the private data of a thread can be accessed by various functions, but it is blocked by other threads.
The thread private data adopts a technology called one-key multi-value, that is, one key corresponds to multiple values. When accessing data, they all access data through key values. It seems that accessing a variable is actually accessing different data. When using thread private data, you must first create an associated key for each thread data. In each thread, this public key is used to represent the thread data, but in different threads, this key represents different data. There are four main functions that operate on the private data of a thread: pthread_key_create (create a key), pthread_setspecific (set the private data of a thread for a key), and pthread_getspecific (read the private data of a thread from a key ), pthread_key_delete (delete a key ). The declaration of these functions is as follows:
# Include <pthread. h>
Int pthread_key_create (pthread_key_t * Key, void (* destr_function) (void *));
Int pthread_setspecific (pthread_key_t key, const void * pointer ));
Void * pthread_getspecific (pthread_key_t key );
Int pthread_key_delete (pthread_key_t key );
Pthread_key_create: allocates an item from the TSD pool of Linux and assigns the value to the key for future access. The first parameter key of pthread_key_create is a pointer to the key value, the second parameter is a function pointer. if the pointer is not empty, destr_function () will be called with the data associated with the key as the parameter when the thread exits to release the allocated buffer.
Once a key is created, all threads can access it, but each thread can enter different values in the key as needed, this is equivalent to providing a global variable with the same name and different values, one-click multi-value. One-click multi-value depends on a key data structure array, that is, the structure of the TSD pool is as follows:
Static struct pthread_key_struct pthread_keys [pthread_keys_max] ={{ 0, null }};
Creating a TSD is equivalent to setting an item in the structure array as "in_use", returning its index to * key, and then setting destructor function destr_function.
Pthread_setspecific: This function associates the pointer value (not content) with the key. When pthread_setspecific is used to specify a new thread data for a key, the thread must first release the original thread data to recycle space.
Pthread_getspecific: use this function to obtain the data associated with the key.
Pthread_key_delete: This function is used to delete a key. The memory occupied by the key will be released. Note that the memory occupied by the key is released, and the memory occupied by the thread data associated with the key is not released. Therefore, the release of thread data must be completed before the release key.
Example 8-4 shows how to create and use the private data of a thread. The Code is as follows.
Example 8-4
# Include <stdio. h>
# Include <string. h>
# Include <pthread. h>

Pthread_key_t key;

Void * thread2 (void * Arg)
{
Int TSD = 5;
Printf ("thread % d is running/N", pthread_self ());
Pthread_setspecific (Key, (void *) TSD );
Printf ("thread % d returns % d/N", pthread_self (), pthread_getspecific (key ));
}

Void * thread1 (void * Arg)
{
Int TSD = 0;
Pthread_t thid2;

Printf ("thread % d is running/N", pthread_self ());
Pthread_setspecific (Key, (void *) TSD );
Pthread_create (& thid2, null, thread2, null );
Sleep (2 );
Printf ("thread % d Return % d/N", pthread_self (), pthread_getspecific (key ));
}

Int main (void)
{
Pthread_t thid1;
Printf ("main thread begins running/N ");
Pthread_key_create (& Key, null );
Pthread_create (& thid1, null, thread1, null );
Sleep (5 );
Pthread_key_delete (key );
Printf ("main thread exit/N ");
Return 0;
}
Compile and execute the command. The result is as follows:
$ Gcc-O 8-4 8-4.c-g-l pthread
$./8-4
Main thread begins running
Thread-1209746544 is running
Thread-1218139248 is running
Thread-1218139248 returns 5
Thread-1209746544 return 0
Main thread exit
Program Description: In the program, the main thread creates thread thread1 and thread thread1 creates thread thread2. The two threads respectively use TSD as the private data of the thread. The program running result shows that the TSD modification of the two threads does not interfere with each other. It can be seen that thread2 ends before thread1, and that after thread2 is created, the thread sleeps 3 S and waits for thread2 to be executed. The main thread sleep for 5s and waits until thread1 ends. It can be seen that the modification of thread2 to TSD does not affect the value of TSD of thread1.

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.