I am a bit confused regarding the thread safety in ReferenceCountedObjectPtr. From what I can read in docs its saids that pointers (ReferenceCountedObjectPtr?) to ReferenceCountedObject objects "can be passed between threads safely".
I understand that ReferenceCountedObject has an atomic counter and access to this counter is thread safe.
However I do not understand how ReferenceCountedObjectPtr can be passed safely between threads since the call to atomic increment of ReferenceCountedObject is not thread safe.
static void incIfNotNull (ReferencedType* o) noexcept { if (o != nullptr) o->incReferenceCount(); }
Here o might be deleted by another thread after the if statement and before the ++refCount state in ReferenceCountedObject, making o corrupt before atomic increment.
The way I read the code is that the ++refCount is an atomic operation, but everything around it is not and therefor ReferenceCountedObjectPtr is not thread safe.
Is this the correct interpretation or am I missing something here?
Stack Overflow question on the same topic
http://stackoverflow.com/questions/8007186/read-write-thread-safe-smart-pointer-in-c-x86-64