I am protecting pointer with mutex for writes already like this
// thread1
if(pointer)
{
boost::mutex::scoped_lock lock(pointer_mutex);
if(pointer)
pointer->DoStuff();
}
// thread2
if(pointer)
{
boost::mutex::scoped_lock lock(pointer_mutex);
if(pointer)
pointer = anotherPointer;
}
// thread3
if(pointer)
{
boost::mutex::scoped_lock lock(pointer_mutex);
pointer = 0;
}
i do not want want to put that mutex outside of the block, because pointer is null 99.999 of the time.
this works fine with no crashes, but i not experienced enough declare it thread safe.
my question is:
Are if(pointer) pointer = 0; pointer = anotherPointer; atomic?
Thank you.
It’s not safe and neither is “double-checked locking”. Please do read this paper.