Meyers version in singleton mode and meyers version in singleton Mode

Source: Internet
Author: User

Meyers version in singleton mode and meyers version in singleton Mode

View the Code directly:

 

/* The Singleton mode ensures that a class has only one instance in a program and provides a global access point for accessing it in the program design, in many cases, make sure that one class has only one instance. For example, in the donepws system, only one window manager and only one log output system and one GUI system class library can exist in a program, there is only one ImageManager */# include <iostream> # include <windows. h> # include <process. h> using namespace std; // Meyers Singleton Pattern implements class CSingleton2 {public: // The singleton object uses local static variables to delay it until it is called. instantiate static CSingleton2 & GetInstance () {static CSingleton2 sg; return sg;} void Print () {printf ("pri Nt Singleton2 count = % d \ n ", m_count);} private: int m_count; // The constructor is privatized to prevent external access, CSingleton2 () {printf ("START construct Singleton2 count = % d \ n", m_count);: Sleep (1000 ); // here is to see the effect m_count = 100; printf ("End construct Singleton2 count = % d \ n", m_count);} public :~ CSingleton2 () {printf ("Call destructor \ n");} private: // CSingleton2 (const CSingleton2 &); CSingleton2 & operator = (const CSingleton2 &) ;}; unsigned int _ stdcall thread (void *) {printf ("current Thread ID = % d \ n ",:: getCurrentThreadId (); CSingleton2: GetInstance (). print (); return 0;} void TestMultThread () {// here three threads are created for (int I = 0; I <3; I ++) {HANDLE t = (HANDLE): _ beginthreadex (NULL, 0, thread, NULL, 0, NULL);: Clos EHandle (HANDLE) t) ;}} int main () {TestMultThread (); getchar (); return 0 ;}/ * 1: advantages: 1 ): this implementation is a "lazy" Singleton mode, which means that it will be instantiated only when GetInstance () is called for the first time. 2): You do not need to call the GetInstance () static method every time, NULL = m_instance must be determined, with a relatively high efficiency. 3): use an object instead of a pointer to allocate memory. Therefore, automatically calling the Destructor will not cause memory leakage. 4 ): in multi-thread mode, only one instance can be generated. 2: disadvantage: in the case of multithreading, it is not really Thread security * // * current Thread ID = 1148 start construct Singleton2 count = 0 --- assume that the 1148 Thread creates a single-piece instance, allocate memory, but the instance member variable current Thread ID = 6668 print Singleton2 count = 0 current Thread ID = 6892 print Singleton2 count = 0 -- Thread 6668 gets ownership, at this time, Singleton2 memory has been allocated, but the member variable has not been initialized, so Print, m_count = 0; end construct Singleton2 count = 100 -- 6892 get thread ownership, initialize the member variables and call the Print function to output 100; print Singleton2 count = 100 call the Destructor-A destructor, which indicates that an instance object Press any key to continue is generated. This is because the C ++ constructor is NOT thread-safe. The constructor in C ++ is simply divided into two steps: Step 1: Memory Allocation Step 2: Initialize the member variable due to the multi-thread relationship, maybe when we have allocated the memory, before initiating the member variable, we need to switch the thread. After another thread obtains the ownership, the variable initialization has not been completed because the memory has been allocated, therefore, the related values of the printed member variables may be inconsistent. Conclusion: although the Meyers method ensures that a unique instance is generated in multiple threads, it cannot ensure that the value of the member variable is correct. */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.