Advertisement
frasl

Untitled

Mar 4th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. std::atomic<Singleton*> Singleton::m_instance;
  2. std::mutex Singleton::m_mutex;
  3.  
  4. Singleton* Singleton::getInstance() {
  5.     Singleton* tmp = m_instance.load(std::memory_order_relaxed);
  6.     std::atomic_thread_fence(std::memory_order_acquire);
  7.     if (tmp == nullptr) {
  8.         std::lock_guard<std::mutex> lock(m_mutex);
  9.         tmp = m_instance.load(std::memory_order_relaxed);
  10.         if (tmp == nullptr) {
  11.             tmp = new Singleton;
  12.             std::atomic_thread_fence(std::memory_order_release);
  13.             m_instance.store(tmp, std::memory_order_relaxed);
  14.         }
  15.     }
  16.     return tmp;
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement