In multithreaded programs, it is possible to frequently encounter problems where static variables used by functions cannot hold their values for different threads. There are a number of ways to solve this, and this article looks at the thread-specific data to solve this problem.
Each system supports a limited number of thread-specific data elements. POSIX requires that this limit be no less than 128 (per process). The system maintains an array of structures for each process that we call the key structure,
The flags in the key structure indicate whether the data element is in use, and all flags are initialized to "not in use." When a thread calls Pthread_key_create to create a new thread-specific data element, the system returns the first element that is not in use. The destructor pointer in the key structure, when a thread terminates, the system scans the thread's Pkey array and invokes the corresponding destructor for each non-empty pkey pointer.
In addition to the process-wide array of key structures, the system maintains multiple messages about each thread within the process. These thread-specific information, which we call the pthread structure, is part of a pointer array of 128 elements that we call the Pkey array.
Note that when we call pthread_key_create to create a key, the system tells us this key. Each thread can then store a value (pointer) for the key, which is usually obtained by each thread through malloc.
The specific functions are as follows:
#include <pthread.h>int pthread_once (pthread_once_t *onceptr, Void (*init) (void)), int pthread_key_create ( pthread_key_t *keyptr, Void (*destructor) (void *value)); void *pthread_getspecific (pthread_key_t key); int Pthread_ Setspecific (pthread_key_t key, const void *value);
Use cases:
#include <stdio.h> #include <stdlib.h> #include <pthread.h>pthread_key_t rl_key;pthread_once_t rl_ once = Pthread_once_init;void destructor (void *value) {printf ("pthread:%d destructor value (%p) \ n", Pthread_self (), Val UE); }void Create_key () {pthread_key_create (&rl_key, destructor) after the thread finishes,//apply for a key printf ("pthread:%d Create_key%d\n ", Pthread_self (), Rl_key);} void *foo (void *param) {int i; for (i = 0; i < 2; i++) {pthread_once (&rl_once, Create_key);//Run only once create_key void *ptr; if (ptr = pthread_getspecific (rl_key)) = = NULL) {//check whether the current thread key field has a value of ptr = malloc (10); Pthread_setspecific (Rl_key, PTR);//Set the value of key printf ("pthread:%d key (%d) value (%p) \ n", Pthread_self (), Rl_key, PTR); }}}int Main (void) {pthread_t t[5]; int i; for (i = 0; i < 5; i++) {pthread_create (&t[i], NULL, foo, NULL); } for (i = 0; i < 5; i++) {Pthread_join (t[i], NULL); } return 0;}
Pthread_once (&rl_once, Create_key)
No matter how many threads are only executed by the first thread that executes it, it is guaranteed that the allocated Rl_key is secure.
if (ptr = pthread_getspecific (rl_key)) = = NULL) {//check whether the current thread key field has a value
Checks whether the key field of the current thread's pthread structure has a value
Pthread_setspecific (Rl_key, PTR);//Set the value of key
Sets the value of the key field for the pthread structure of the current thread
void destructor (void *value) { printf ("pthread:%d destructor value (%p) \ n", pthread_self (), value); Free (value);//After thread end execution}
When the thread ends, the destructor is automatically dropped and the allocated resource is freed.
The results of the above example run as follows:
pthread:1691465472 create_key 0pthread:1691465472 Key (0) value (0x7f2b5c0008c0) pthread:1680975616 key (0) value ( 0X7F2B540008C0) pthread:1659995904 key (0) value (0x7f2b440008c0) pthread:1659995904 destructor value (0X7F2B440008C0) pthread:1680975616 destructor Value (0x7f2b540008c0) pthread:1691465472 destructor value (0X7F2B5C0008C0) pthread : 1649506048 key (0) value (0x7f2b3c0008c0) pthread:1649506048 destructor value (0x7f2b3c0008c0) pthread:1670485760 key (0 ) value (0x7f2b4c0008c0) pthread:1670485760 destructor value (0X7F2B4C0008C0)
Thread-specific data