Thread-local storage (TLS) is a mechanism by which variables are allocated such that there is one instance of the variable per extant thread.
There are two ways to use TLS.
#Use the keyword “_thread” provided by compiler
In the following example, “_thread” keyword which is supported by gcc compiler
to tell the two integer variables “TLS_data1” and TLS_data2” are TLS varaibles.
Test and verify it:
#Use “pthread_create_key() / pthread_delete_key()” to for bug chunk of data
Since the storage area for TLS is limited, and if there is a big chunk of datas/pbjects are to be stored in TLS, it can be organized into a struct and use “pthread_create_key() /pthread_delete_key()”.
Compile and run it to verify: