[10 of C/C ++ multi-thread programming] private data of pthread threads

Source: Internet
Author: User
Tags pthread mutex

Thread private data for multi-thread programming
PthreadPOSIX threads is short for POSIXThread Standard.Synchronization of threads from mutex [6 of C/C ++ multi-thread programming] pthread mutex, semaphore [7 of C/C ++ multi-thread programming] pthread semaphores, conditional Variable [8 of C/C ++ multi-thread programming] pthread conditional variable, read/write lock [9 of C/C ++ multi-thread programming] pthread read/write lock, the multi-thread synchronization mechanism has been thoroughly explored,The essence of multi-threaded programming,In-depth understanding is required.Thread-specific Data (TSD), mentioned in the deep understanding of [C/C ++ multi-Thread programming 5] pthread. global variables of processes are shared by all threads, in terms of functionality and security, it is necessary to provide global variables for threads.. The global variable of the thread is the private data of the thread, which is only valid within a thread.1. Private Data of the thread;
The private data of a thread is identified by the associated key. When the thread is created, the private data is associated with the key. The key is used to write content to the private data of the thread, and the key is used to read the private data of the thread, finally, the private data of the thread is deleted using the key.After the thread private data is created, all threads in the process can use this key to write and read data to the thread private data. For different threads, the same key value accesses the private data of the thread.Example:After the thread private data associated with the key is created, each thread has a corresponding thread private data.Thread A uses the key to access the private data of the corresponding thread in thread.Thread B uses the key to access the private data of the corresponding thread in thread B.
2. Basic functions of thread private data;# Include <pthread. h>Create thread private data:Intpthread_key_create (pthread_key_t * key, void (* destr_function) (void *));The first parameter of the function is the key value key, and the second parameter is the destroy function (generally set to NULL, when not empty, when the thread private data is destroyed, will call this function to release the allocated memory ).Write Data:
Intpthread_setspecific (pthread_key_t key, void * pointer );The first parameter of the function is the key value key, and the second parameter is the data pointer to write (this pointer type is void *, which can be written to any data type pointer ). Pointer writes the pointer value instead of the content pointed to by pointer.Read data:
Void * pthread_getspecific (pthread_key_t key );The parameter of this function is the key value, and the written data is returned.Destroy the private data of a thread:Intpthread_key_delete (pthread_key_t key );This function is used to destroy private data of a thread.
3. Cow test:The child1 and child2 threads write their own thread IDs to their own private data, write a pointer, and read the data as a void * pointer, type conversion is required.

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.