title: Are function static variables thread-safe in GCC?
body: <p>In the example code</p>
  
  <pre><code>void foo()
  {
    static Bar b;
    ...
  }
  </code></pre>
  
  <p>compiled with <strong>GCC</strong> is it guaranteed that <code>b</code> will be created and initialized in a thread-safe manner ?</p>
  
  <p>In gcc's man page, found the <strong>-fno-threadsafe-statics</strong> command line option:</p>
  
  <blockquote>
    <p>Do not emit the extra code to use the
    routines specified in the C++ ABI for
    thread-safe initialization of local
    statics. You can use this option to
    reduce code size slightly in code that
    doesn't need to be thread-safe.</p>
  </blockquote>
  
  <ol>
  <li><p>Does it mean, that local statics are thread-safe by default with GCC ? So no reason to put explicit guarding e.g. with <code>pthread_mutex_lock/unlock</code> ?</p></li>
  <li><p>How to write portable code - how to check if compiler will add its guards ? Or is it better to turn off this feature of GCC ?</p></li>
  </ol>