title: Specify construction/destruction order of static locals in different accessors
body: <p>I'm experiencing a crash in <code>cxa_finalize</code> running a program (this is a program, and not a library within):</p>
  
  <pre><code>$ ./ac-test.exe 
  Assertion failed: AcLock.cpp(54): AcLock
  libc++abi.dylib: terminate called without an active exception
  Abort trap: 6
  </code></pre>
  
  <p>The assertion/crash is due to the interaction between an object and a logger. The logger is destructed before the object, but the object uses the logger. So the mutex being acquired that pops the assert or crash has already been destroyed (hence the reason <code>pthread_mutex_lock</code> fails when locking the logger).</p>
  
  <p>I've read the GCC manual on <a href="http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html" rel="nofollow">Specifying Attributes of Variables</a> and <a href="http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html" rel="nofollow">Declaring Attributes of Functions</a>, but I'm obviously missing something.</p>
  
  <p>I placed the object and logger in a common header within accessors and tried to specify a construction order:</p>
  
  <pre><code>// AcGlobals.h
  static AcLogger&amp; GetLogger() {
  
      static AcLogger logger __attribute__(init_priority(50));
      return logger;
  }
  static AcSocketList&amp; GetAcceptSockets() {
  
      static AcSocketList sockets __attribute__(init_priority(100));
      return sockets;
  }
  </code></pre>
  
  <p>That resulted in a bunch of errors:</p>
  
  <pre><code>./AcGlobals.h:24:46: error: expected ';' at end of declaration
      static AcLogger logger __attribute__((init_priori...
  
  ./AcGlobals.h:24:47: warning: declaration does not declare anything
        [-Wmissing-declarations]
      static AcLogger logger __attribute__((init_priori...
  </code></pre>
  
  <p>I also tried placing the attribute on the function rather than the variable:</p>
  
  <pre><code>// AcGlobals.h
  static AcLogger&amp; GetLogger() __attribute__(init_priority(50)) {
  
      static AcLogger logger;
      return logger;
  }
  static AcSocketList&amp; GetAcceptSockets() __attribute__(init_priority(100)) {
  
      static AcSocketList sockets;
      return sockets;
  }
  </code></pre>
  
  <p>This resulted in more problems:</p>
  
  <pre><code>./AcGlobals.h:22:53: warning: GCC does not allow init_priority attribute in this
        position on a function definition [-Wgcc-compat]
  static AcLogger&amp; GetLogger() __attribute__((init_priority(50))) {
                                                ^
  ./AcGlobals.h:22:53: error: can only use 'init_priority' attribute on file-scope
        definitions of objects of class type
  </code></pre>
  
  <p>I also tried <code>__attribute__((constructor(50)))</code> instead of <code>init_priority</code> with no joy.</p>
  
  <p><em>Note</em>: I'm working on an Apple machine. Apple has a 'feature' where constructor priority only applies to adorned functions and variables within the same file. So these cannot be spread out across translation units.</p>
  
  <p>How, precisely, do I specify the order of construction and destruction of local static objects?</p>