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.