Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::atomic<Singleton*> Singleton::m_instance;
- std::mutex Singleton::m_mutex;
- Singleton* Singleton::getInstance() {
- Singleton* tmp = m_instance.load(std::memory_order_relaxed);
- std::atomic_thread_fence(std::memory_order_acquire);
- if (tmp == nullptr) {
- std::lock_guard<std::mutex> lock(m_mutex);
- tmp = m_instance.load(std::memory_order_relaxed);
- if (tmp == nullptr) {
- tmp = new Singleton;
- std::atomic_thread_fence(std::memory_order_release);
- m_instance.store(tmp, std::memory_order_relaxed);
- }
- }
- return tmp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement