Can two stores be reordered in such singleton implementation?
Can two stores be reordered in such singleton implementation? In the following singleton 'get' function, can other threads see instance as not-null, but almost_done still false? (Say almost_done is initially false .) instance almost_done almost_done false Singleton *Singleton::Get() { auto tmp = instance.load(std::memory_order_relaxed); std::atomic_thread_fence(std::memory_order_acquire); if (tmp == nullptr) { std::lock_guard<std::mutex> guard(lock); tmp = instance.load(std::memory_order_relaxed); if (tmp == nullptr) { tmp = new Singleton(); almost_done.store(true, std::memory_order_relaxed); // 1 std::atomic_thread_fence(std::memory_order_release); instance.store(tmp, std::memory_order_relaxed); // 2 } } return tmp; } If they can, why? What's the rationale? I know nothing can "get out" of an acquire-release section, but can't 2 enter it and be reorder...