How to perform double buffering with atomic pointers? : <p>Atomic newbie here. My code currently looks like this (simplified):</p>

<pre class="lang-cpp prettyprint-override"><code>std::atomic&lt;Object*&gt; object;

void thread_a()
{
  object.load()-&gt;doSomething(); // (1)
}

void thread_b()
{
    Object* oldObject = object.load();
    Object* newObject = new Object();
    // Update new object accordingly...

    while (!object.compare_exchange_weak(oldObject, newObject));

    delete oldObject;
}
</code></pre>

<p>In words, my idea is to let <code>thread_b</code> atomically swap the shared object (double-buffering), while <code>thread_a</code> performs some work on it. My question: can I safely assume that the shared object will be "protected" against data races while <code>thread_a</code> calls <code>doSomething()</code> on it, as done in (1)? </p>
