title: Is it safe to implement Double Check Lock Pattern in C++ before C++11
body: <pre><code>class SelfTesting
  {
  private:
      char * pChar;
      SelfTesting()
      {
          pChar = new char[1024 * 1024 * 1024];
      }
  public:
      static SelfTesting * pSelf;
  
      static SelfTesting&amp; GetInst()
      {
          if (!pSelf)
          {
              boost::lock_guard&lt;boost::mutex&gt; lock(g_boost_mutex);
              if (!pSelf)
              {
                  pSelf = new SelfTesting;
              }
          }
          return *pSelf;
      }
  };
  </code></pre>
  
  <p>Generally, I know the problem is caused by:<br></p>
  
  <pre><code>1. allocate memory for `SelfTesting`
  2. store the pointer to the allocated memory in `pChar`
  3. initialize `SelfTesting`
  </code></pre>
  
  <ol>
  <li>If other thread touch the pointer between step 2 and 3, data race condition happens. <a href="https://stackoverflow.com/questions/8570903/how-to-understand-double-checked-locking">From</a></li>
  <li>If the pointer copying is not atomic, data race condition can also happens. <a href="https://stackoverflow.com/questions/12128706/does-this-redeem-a-thread-safe-double-checked-locking-pattern">From</a></li>
  </ol>
  
  <p>I know that I can use local static variable to implement this pattern in C++11. My question is that is the above implementation thread safe or it is undefined when I am using C++ standard before C++11. Is <code>boost::mutex</code> make sure that <code>pSelf</code> will update after <code>lock</code> is died?<br></p>