Pthread Thread Private Data

Source: Internet
Author: User
Tags posix

multithreaded Programmingthe thread private data
      Pthread is a short name for POSIX threads. It's POSIX. Threading Standards. thread synchronization from mutual exclusion amount of "C + + multithreaded programming Six" pthread Mutual rejection amount , signal Volume + + multi-threaded programming seven "pthread semaphore , condition variable " multithreaded programming of C/W + + "pthread condition variable , read/write lock Multithreaded programming Nine "pthread read-write lock . Multi-threading synchronization mechanism has a clear and in-depth exploration, the essence of multithreaded programming, need to understand deeply.         Thread Private Data TSD (thread-specific), which is mentioned in the thread-in-depth understanding of the "V/C + + multithreaded programming Pthread"Process Global Variablesis shared by all threads and is in a functional and secure need. It is necessary to provide a threadThread Global Variables . Thread global variables are thread-private data that is only valid within a thread.

for different threads, the same key value, separate access to the thread's own private data, do not affect each other. Example: After creating the thread-private data associated with the key. Each thread has a corresponding thread-private data. thread A uses key to access the corresponding thread-private data in thread A.

thread B uses key to access the corresponding thread-private data in thread B.
2. Thread private data basic functions . #include <pthread.h>To Create a thread-private data : intpthread_key_create(pthread_key_t *key, Void (*destr_function) (void *) c6>); the first parameter of the function is the key value key, and the second parameter is the Destroy function (which is generally set to null). When not empty, the function is called to free allocated memory when the thread's private data is destroyed. 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 write data pointer (the pointer type is void*, which can write pointers to whatever data type). Pointer writes the value of this pointer. Rather than the content that pointer points to. read Data :
void *pthread_getspecific(pthread_key_t key); The function's parameters are the key value keys. Returns the data written.

destroying thread-private data : intpthread_key_delete(pthread_key_t key); The function's parameters are key-value key, which is used to destroy thread-private data.
3. Kind : Thread Child1,child2 writes its own thread ID to its own thread-private data, and writes a pointer to the read-out data as a void * type pointer. It needs to be coerced into a type cast.
      

Pthread Thread Private Data

Span style= "font-size:18px" >                    1.  thread private data ;
        thread private data through the associated Span style= "COLOR: #009900" > key value key recognition, when created, associates private data with key, writes content to thread private data via key, It also reads the contents of the thread's private data through key, and finally deletes the thread private data via key.           Thread Private data is created, the process All threads Can use this key to write and read data to the thread's private data.

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.