Implementation of Double Checked Locking in C++ 98/03 using volatile
<p>Reading <a href="http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf" rel="nofollow">this</a> article about Double Checked Locking Pattern in C++, I reached the place (page 10) where the authors demonstrate one of the attempts to implement DCLP "correctly" using <code>volatile</code> variables:</p>

<pre><code>class Singleton {
public:
  static volatile Singleton* volatile instance();

private:
  static volatile Singleton* volatile pInstance;
};

// from the implementation file
volatile Singleton* volatile Singleton::pInstance = 0;
volatile Singleton* volatile Singleton::instance() {
  if (pInstance == 0) {
    Lock lock;
    if (pInstance == 0) {
      volatile Singleton* volatile temp = new Singleton;
      pInstance = temp;
    }
  }
  return pInstance;
}
</code></pre>

<p>After such example there is a text snippet that I don't understand:</p>

<blockquote>
  <p>First, the Standard’s constraints on observable behavior are only for
  an abstract machine defined by the Standard, and that abstract machine
  has no notion of multiple threads of execution. As a result, though
  the Standard prevents compilers from reordering reads and writes to
  volatile data <em>within</em> a thread, it imposes no constraints at all on
  such reorderings <em>across</em> threads. At least that’s how most compiler
  implementers interpret things. As a result, in practice, many
  compilers may generate thread-unsafe code from the source above.</p>
</blockquote>

<p>and later:</p>

<blockquote>
  <p>... C++’s abstract machine is single-threaded, and C++ compilers may
  choose to generate thread-unsafe code from source like the above,
  anyway.</p>
</blockquote>

<p>These remarks are related to the execution on the uni-processor, so it's definitely not about cache-coherence issues.</p>

<p>If the compiler can't reorder reads and writes to volatile data <em>within</em> a thread, how can it reorder reads and writes <em>across</em> threads for this particular example thus generating thread-unsafe code?</p>