title: Threadsafe lazy initialization: static vs std::call_once vs double checked locking
body: <p>For threadsafe lazy initialization, should one prefer a static variable inside a function, std::call_once, or explicit double checked locking? Are there any meaningful differences?</p>
  
  <p>All three can be seen in this question.</p>
  
  <p><a href="https://stackoverflow.com/questions/6086912/double-checked-lock-singleton-in-c11">Double-Checked Lock Singleton in C++11</a></p>
  
  <p>Two versions of double checked locking in C++11 turn up in Google.</p>
  
  <p>Anthony Williams <a href="http://www.justsoftwaresolutions.co.uk/threading/multithreading-in-c++0x-part-6-double-checked-locking.html" rel="noreferrer">shows</a> both double checked locking with explicit memory ordering and std::call_once. He doesn't mention static but that article might have been written before C++11 compilers were available.</p>
  
  <p>Jeff Preshing, in an extensive <a href="http://preshing.com/20130930/double-checked-locking-is-fixed-in-cpp11/" rel="noreferrer">writeup</a>, describes several variations of double checked locking. He does mention using a static variable as an option and he even shows that compilers will generate code for double checked locking to initialize a static variable. It's not clear to me if he concludes that one way is better than the other.</p>
  
  <p>I get the sense that both articles are meant to be pedagogical and that there's no reason to do this. The compiler will do it for you if you use a static variable or std::call_once.</p>