Thread Specific Data vs Thread Local Storage

后端 未结 2 2011
耶瑟儿~
耶瑟儿~ 2020-12-14 21:12

I\'ve read Kerrisk\'s The Linux Programming Interface: A Linux and UNIX System Programming Handbook, Chapter 31 on Threads. The chapter include Thread Specific Data (Section

相关标签:
2条回答
  • 2020-12-14 21:12

    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.

    0 讨论(0)
  • 2020-12-14 21:18

    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 dlopened shared library, see this bug), because its use does not involve malloc (with the same exception).

    0 讨论(0)
提交回复
热议问题