I've read the question and answers here, and learned that a true thread-safe singleton can be implemented with correctly placed memory barrier, which is stated here in chapter 6. I've come up with my own version of singleton implementation, by reviewing the code I've seen no memory ordering problem so far, but it differs a lot from a typical DCLP approach. Do you think this is a reliable implementation?
static std::atomic<T*> _instance;
...
Singleton* get_instance() {
ins = _instance.load(std::memory_order_acquire);
if (ins == nullptr) {
T* temp = nullptr;
{
std::lock_guard<std::mutex> guard(_lock);
temp = new(std::nothrow) T;
}
if(!_instance.compare_exchange_strong(nullptr, temp, std::memory_order_release)) {
delete temp;
}
}
return _instance.load(std::memory_order_acquire);
}