Atomic newbie here. My code currently looks like this (simplified):
std::atomic<Object*> object;
void thread_a()
{
object.load()->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;
}
In words, my idea is to let thread_b atomically swap the shared object (double-buffering), while thread_a performs some work on it. My question: can I safely assume that the shared object will be "protected" against data races while thread_a calls doSomething() on it, as done in (1)?