Advertisement
frasl

Untitled

Mar 4th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.40 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();
  6.     if (tmp == nullptr) {
  7.         std::lock_guard<std::mutex> lock(m_mutex);
  8.         tmp = m_instance.load();
  9.         if (tmp == nullptr) {
  10.             tmp = new Singleton;
  11.             m_instance.store(tmp);
  12.         }
  13.     }
  14.     return tmp;
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement