Thread Specific Data vs Thread Local Storage
The pthread_key_create
and friends are much older, and thus supported on more systems.
The __thread
is a relative newcomer, is generally much more convenient to use, and (according to Wikipedia) is supported on most POSIX systems that still matter: Solaris Studio C/C++, IBM XL C/C++, GNU C, Clang and Intel C++ Compiler (Linux systems).
The __thread
also has a significant advantage that it is usable from signal handlers (with the exception of using __thread
from dlopen
ed shared library, see this bug), because its use does not involve malloc
(with the same exception).
The pthread interfaces are POSIX standard, so they are more portable. Use them if you intend to use the code on something besides a linux system. On the other hand, if you are strictly on gcc/linux, then the __thread mechanism is certainly easier to use. Just be aware that it is a gcc specific extension, and not supported on all platforms.