What is the difference between declaring a thread-local variable using the dedicated keyword:
_Thread_local int var;
And using the specific tss_
set of functions:
tss_t key;
tss_create(&key, free);
tss_set(key, malloc(sizeof(int)));
int* pVar = tss_get(key);
From what I understand, the _Thread_local
keyword declares a variable with thread storage duration, while the tss_
set of functions return a key to the calling thread. This key can then be used to access some global heap memory which can be allocated as needed, and that pointer will only be available to that thread, am I right?
Functionally, the important difference is the establishment of a destructor. In your example this is free
, but in fact it could be any function with the right signature.
So this gives the possibility for a callback at the end of any thread for doing any kind of cleanup.