Single-instance model: lazy model: Regular edition; lazy model: Regular edition

Source: Internet
Author: User

Single-instance model: lazy model: Regular edition; lazy model: Regular edition

Directly run the Code:

 

/* 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 "C1.H" # include <windows. h> # include <process. h> using namespace std; class CSingleton1 {public: // obtain the static function of the instance object static function static CSingleton1 * GetInstance () {if (NULL = m_instance) {m_instance = new CSingleton1 ();} return m_instance;} // release the static v memory. Oid ReleaseInstance () {if (NULL! = M_instance) {delete m_instance; m_instance = NULL; printf ("release memory \ n") ;}// test the function void Print () {printf ("print out CSingleton1 \ n");} protected: private: // The constructor is private, so that external access is not allowed, so that only one instance object can be accessed. CSingleton1 () {printf ("CSingleton1 Begin Construct \ n");: Sleep (1000); // The result is printf ("CSingleton1 End Construct \ n ");} // destructor-virtual function to prevent the sub-class from only calling the parent class's destructor without calling the sub-class. Virtual ~ CSingleton1 () {printf ("CSingleton1 Destruct \ n");} private: // CSingleton1 (const CSingleton1 &) to prevent copy construction and assign values &); CSingleton1 & operator = (const CSingleton1 &); private: static CSingleton1 * m_instance;}; CSingleton1 * CSingleton1: m_instance = NULL; unsigned int _ stdcall thread (void *) {printf ("current Thread ID = % d \ n",: GetCurrentThreadId (); CSingleton1: GetInstance ()-> Print ();/* CSingleton1 :: releaseInstance (); we can no longer thread When the ReleaseInstance () function is called, the program will crash */return 0;} void TestMultThread () {// three threads for (int I = 0; I <3; I ++) {HANDLE t = (HANDLE): _ beginthreadex (NULL, 0, thread, NULL, 0, NULL);: CloseHandle (HANDLE) t) ;}} int main () {TestMultThread (); getchar (); return 0 ;}/ * print the running result: current Thread ID = 7708 current Thread ID = 6732 -- both threads obtain ownership and determine that m_instance is NULL. Therefore, the constructor is called to allocate memory. The memory has not been urgently allocated. CSingleton1 Begin Construct CSingleton1 Begin Construct current Thread ID = 1700 -- the third Thread obtains ownership and determines whether m_instance is NULL, therefore, the new operator is called for memory allocation and initialization of the member variables CSingleton1 Begin Construct CSingleton1 End Construct print out CSingleton1 CSingleton1 End Construct print out CSingleton1 -- the result is that three CSingleton1 instance, violation of the singleton mode rule: the entire application has only one class instance 1: We create 3 Auxiliary threads, plus the main thread, there are a total of 4 threads 2: We call the GetInstance () Static Method in each auxiliary thread, because each thread calls back the function to execute very fast, as a result, each thread returns TRUE when determining NULL = m_instance. As a result, each thread callback function creates a CSingleton1 Instance Object and returns a pointer to the object. 3: we have no way to release the CSingleton1 memory, because in multithreading, we do not know whether to create an instance or two or three. * // * 1: Advantages: this implementation is a "lazy" Singleton mode, which means that the memory is allocated only when the GetInstance () static method is called for the first time. If the whole program does not call this static method, no memory will be allocated. It corresponds to the "Hungry Man" Singleton Mode 2: disadvantages: 1) Although the "lazy" mode has advantages, every time you call the GetInstance () static method, NULL = m_instance must be determined to increase the program's relative overhead. 2) due to the use of pointer dynamic memory allocation, We must manually call the ReleaseInstance () static method at the end of the program, release the memory. 3) The biggest drawback is that the thread is unsafe. According to the definition of this mode, the entire application can have only one instance of this class, whether it is a single thread or multiple threads. Multiple instances are generated in multiple threads, which leads to incorrect Running code and Memory leakage. */

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.