How to realize the lazy mode and hungry man model of C++ singleton model
In this article, the editor introduces in detail "how to realize the lazy mode and hungry mode of C++ singleton mode". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to realize the lazy mode and hungry man mode of C++ singleton mode" can help you solve your doubts.
Lazy man mode
The lazy pattern is not instantiated until the first time the class instance is used, but the object of this class does not exist until the getInstance function is called. The slacker itself is not thread-safe.
# include using namespace std;class Singelton {private: Singelton () {m_count + +; printf ("Singelton begin\ n"); Sleep (1000); / / add sleep to magnify the effect printf ("Singelton end\ n");} static Singelton * single;// defines a unique pointer to the instance and is private public: static Singelton * GetSingelton () / / define a public function that can get the unique instance static void print (); static int mcount;}; / / initialize the unique pointer to the instance to nullptrSingelton * Singelton::single = nullptr;int Singelton::m_count = 0 single Singelton * Singelton::GetSingelton () {if (single = = nullptr) {/ / determine whether it is the first time to use single = new Singelton;} return single } void Singelton::print () {cout