Pthread Specific Data In Linux System

58 Views Asked by At

In the Linux System we can use pthread for multithread programming and in the pthread there was a pthread specific data where we should first create a key. But what is the purpose of pthread specific data, i have done use google and chatgpt to knowing for what is this but i don't understand. Does this same like thread local storage ?

1

There are 1 best solutions below

3
John Bollinger On

in the pthread there was a pthread specific data where we should first create a key.

I take you to mean data accessed via pthread_setspecific() and pthread_getspecific(), using keys created via pthread_key_create().

Does this same like thread local storage ?

Yes, pthreads thread-specific data is a form of thread-local storage. The data accessed via a given key is specific to the thread in which it is accessed. Different threads each access their own.

Additionally, it is possible to associate a destructor function with the key, which will be used, separately by each thread, to clean up the object associated with that key in that thread when the thread terminates.

Since C11, standard C has the _Thread_local qualifier that provides a different, simpler mechanism, but does not have a provision for destructor function.