title: Is std::mutex trivially destructible?
body: <p>I have a function <code>foo()</code> protected by a mutex <code>m</code> which is defined as a local static variable of <code>foo()</code>. I'd like to know whether it's safe to call <code>foo()</code> in the destructor of an object <code>bar</code> with static storage duration:</p>
  
  <pre><code>// foo.h
  void foo();
  
  // foo.cpp
  #include "foo.h"
  #include &lt;mutex&gt;
  void foo()  {
      static std::mutex m;
      std::lock_guard&lt;std::mutex&gt; lock(m);
      // ...
  }
  
  // bar.h
  struct Bar { ~Bar(); };
  extern Bar bar;
  
  // bar.cpp
  #include "bar.h"
  #include "foo.h"
  Bar::~Bar() { foo(); }
  Bar bar;
  
  // main.cpp
  int main() {
      Bar bar;
      return 0;
  }
  </code></pre>
  
  <p>If <code>std::mutex</code> is trivially destructible, this should be safe, because <code>bar</code> will be destructed before <code>m</code>. On GCC 5.4, Ubuntu 16.04, calling <code>std::is_trivially_destructible&lt;std::mutex&gt;::value</code> returns <code>true</code>, so it seems okay at least in this compiler. Any definitive answer?</p>
  
  <p>Related: Google C++ Style Guide on <a href="https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables" rel="nofollow noreferrer">Static and Global Variables</a></p>
  
  <hr>
  
  <p><strong>Edit</strong></p>
  
  <p>Apparently, I wasn't clear enough and should have provided more context. Yes, the underlying problem is that I want <code>bar</code> to be destructed before <code>m</code>. This is the "destruction" part of the well known "static initialization fiasco problem", see for example:</p>
  
  <p><a href="https://isocpp.org/wiki/faq/ctors#construct-on-first-use-v2" rel="nofollow noreferrer">https://isocpp.org/wiki/faq/ctors#construct-on-first-use-v2</a></p>
  
  <blockquote>
    <p>The point is simple: if there are any other static objects whose
    destructors might use ans after ans is destructed, bang, you’re dead.
    If the constructors of a, b and c use ans, you should normally be okay
    since the runtime system will, during static deinitialization,
    destruct ans after the last of those three objects is destructed.
    However if a and/or b and/or c fail to use ans in their constructors
    and/or if any code anywhere gets the address of ans and hands it to
    some other static object, all bets are off and you have to be very,
    very careful.</p>
  </blockquote>
  
  <p>This is why Google recommends against using static objects, <em>unless they are trivially destructible</em>. The thing is, if the object is trivially destructible, then the order of destruction doesn't really matter. Even if <code>m</code> is "destructed" before <code>bar</code>, you can still in practice use <code>m</code> in the destructor of <code>bar</code> without crashing the program, because the destructor effectively does nothing (it doesn't deallocate any memory or release any other type of resources).</p>
  
  <p>And in fact, if <code>m</code> is trivially destructible, then the program may not even destruct <code>m</code> at all, which actually ensures that <code>m</code> is "destructed" after <code>bar</code> or any other static objets which are not trivially destructible. See for instance:</p>
  
  <p><a href="http://en.cppreference.com/w/cpp/language/lifetime#Storage_reuse" rel="nofollow noreferrer">http://en.cppreference.com/w/cpp/language/lifetime#Storage_reuse</a> </p>
  
  <blockquote>
    <p>A program is not required to call the destructor of an object to end
    its lifetime if the object is trivially-destructible or if the program
    does not rely on the side effects of the destructor.</p>
  </blockquote>
  
  <p>For these reasons, it is in practice overkill to use complex singleton idioms such as the <a href="https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Nifty_Counter" rel="nofollow noreferrer">Nifty Counter idiom</a> if your singleton is trivially destructible.</p>
  
  <p>In other words, if <code>std::mutex</code> is trivially destructible, then my code sample above is safe: <code>m</code> is either destructed after <code>bar</code>, or it is "technically destructed" before <code>bar</code> but wouldn't cause a crash anyway. However, if <code>std::mutex</code> is not trivially destructible, then I may need to use the Nifty Counter idiom instead, or alternatively the simpler but "purposefully leaking" <a href="https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use" rel="nofollow noreferrer">Trusty Leaking idiom</a>.</p>
  
  <p>Related:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/a/335746/1951907">https://stackoverflow.com/a/335746/1951907</a></li>
  <li><a href="https://stackoverflow.com/a/17712497/1951907">https://stackoverflow.com/a/17712497/1951907</a></li>
  </ul>