Here is a piece of code that is DCL (double-checked locking) implemented by ‘Sequentially Consistent Atomics’ semantics in C++. The code is as follows:
std :: atomic <Singleton *> Singleton :: m_instance;
std :: mutex Singleton :: m_mutex;
Singleton * Singleton :: getInstance () {
Singleton * tmp = m_instance.load (); // 3
if (tmp == nullptr) {
std :: lock_guard <std :: mutex> lock (m_mutex);
tmp = m_instance.load ();
if (tmp == nullptr) {
tmp = new Singleton; // 1
m_instance.store (tmp); // 2
}
}
return tmp;
}
I think: '1' includes not only read and write commands, but also call commands, then the call commands may be reordered behind '2'; then '3' may get an unsafe 'tmp' pointer.
So, I want to ask: Is the above code thread-safe?