HOWTO choose a threading model with GCC
The __thread
marker available with recent revisions of the C
standard is very convenient, but comes at a price. The lookup procedure for
thread-local variables can be expensive. Some efficiency can be gained by
choosing an appropriate thread model. But I have to choose one so
infrequently that I always have to go read a long document every time to
remind myself how to choose one. Instead, here are some succint rules for
choosing the value x
in
__attribute__((tls_model(x)))
when using gcc. The most efficient
models are listed first.
- local-exec
- The variable is built into the executable (versus a dynamically loaded object), and is only accessed from code that is built into the executable.
- initial-exec
- The variable is built into the executable.
- local-dynamic
- The variable is
static
. - global-dynamic
- There are no restrictions on the variable.