Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In C++11 it is thread safe:</p> <blockquote> <p>§6.7 [stmt.dcl] p4 If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.</p> </blockquote> <p>In C++03: </p> <ul> <li>Under g++ it is thread safe.<br> But this is because g++ explicitly adds code to guarantee it.</li> </ul> <p>One problem is that if you have two singletons and they try and use each other during construction and destruction.</p> <p>Read this: <a href="https://stackoverflow.com/questions/335369/finding-c-static-initialization-order-problems#335746">Finding C++ static initialization order problems</a></p> <p>A variation on this problem is if the singleton is accessed from the destructor of a global variable. In this situation the singleton has definitely been destroyed, but the get method will still return a reference to the destroyed object.</p> <p>There are ways around this but they are messy and not worth doing. Just don't access a singleton from the destructor of a global variable.</p> <p>A Safer definition but ugly:<br> I am sure you can add some appropriate macros to tidy this up</p> <pre><code>SomeBaseClass &amp;SomeClass::GetInstance() { #ifdef _WIN32 Start Critical Section Here #elif defined(__GNUC__) &amp;&amp; (__GNUC__ &gt; 3) // You are OK #else #error Add Critical Section for your platform #endif static SomeClass instance; #ifdef _WIN32 END Critical Section Here #endif return instance; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload