title: How does the gcc `__thread` work?
body: <p>How is <code>__thread</code> in gcc implemented? Is it simply a wrapper over <code>pthread_getspecific</code> and <code>pthread_setspecific</code>?</p>
  
  <p>With my program that uses the posix API for TLS, I'm kind of disappointed now seeing that 30% of my program runtime is spent on <code>pthread_getspecific</code>. I called it on the entry of each function call that needs the resource. The compiler doesn't seem to optimize out <code>pthread_getspecific</code> after inlining optimization. So after the functions are inlined the code is basically searching for the correct TLS pointer again and again to get the same pointer returned.</p>
  
  <p>Will <code>__thread</code> help me in this situation? I know that there is <code>thread_local</code> in C11, but the gcc I have doesn't support it yet. (But now I see that my gcc does support <code>_Thread_local</code> just not the macro.)</p>
  
  <p>I know I can simply test it and see. But I have to go somewhere else now, and I'd like to know better on a feature before I attempt a quite big rewrite.</p>