It is the following piece of code
#include <memory>
#include <iostream>
#include <atomic>
class A
{
public:
A * get()
{
if(nullptr == ptr.load(std::memory_order_acquire))
{
ptr.store(new A(),std::memory_order_release);
}
return ptr.load(std::memory_order_acquire);
}
inline static std::atomic<A*> ptr = nullptr;
};
int main()
{
return 0;
}
Reading some documents the code above does not guaranteed that the ptr is not null when get is called . Actually the problem is when ThreadA check that ptr is null enter in CS and before call store ThreadB , enters in CS , allocation memory and returns. When threadA stores the values overwrite the previous one value. When get return the ptr is null. My question is why thread A does not see the updated value ?