3 ms·
__thread is essential for some applications, what's wrong with it?
by bonzini 4y ago
__thread is essential for some applications, what's wrong with it?
- sylware 4y agoTo access TLS-ized system variables (for instance errno if I really need to, I would use the sysv ABI __tls_get_addr() function). But in the application domain, I would use pthread TLS.
- bonzini 4y agoThat's much much slower though.
- sylware 4y agoYou would have to cache the system TLS address in the application pthread TLS.
- bonzini 4y agoNo, pthread TLS is the slower one. Accessing system TLS can be done fast since lib knows the details of the implementation and, worst case, it can use inline assembly. Also, pthread TLS adds a pointer dereference to each access.
- sylware 4y agoThe compiler does not know the inner layout of how the system TLS variables are stored (could be musl way, bionic way, glibc way, etc). It only knows that for system TLS variables it has to go thru __tls_get_addr(), stated by the sysv ABI. Indeed, you are not forced to use pthread TLS to cache the value, you can use your own thread allocated memory, or keep it around on the stack/regs. On x86_64, once you have resolved the address with __tls_get_addr() you can use the same address value for all threads, then use common to all thread storage. I think we agree.