Difference between "thread local storage" and "thread specific storage"

840 views Asked by At

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?

1

There are 1 answers

0
Jens Gustedt On BEST ANSWER

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.