Created
November 4, 2016 15:30
-
-
Save zno5/a170304d0d62b80c2e6e23a395a3362a to your computer and use it in GitHub Desktop.
singleton class using std::call_once
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mutex> | |
class MySingleton { | |
public: | |
static MySingleton * GetInstance() { | |
std::call_once( | |
m_flag, | |
[] () { m_instance = new MySingleton(); } | |
); | |
return m_instance; | |
} | |
private: | |
static MySingleton * m_instance; | |
static std::once_flag m_flag; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment