C ++ thread-safe Singleton Mode

Source: Internet
Author: User

Not much nonsense, commonly usedCodeAccumulated.

I. Lazy mode: a new class instance is generated when the class instance is called for the first time, and only this instance is returned in the future.

You need to use a lock to ensure thread security: the reason is that multiple threads may enter the if statement to determine whether an instance already exists, so that non-thread safety.

Double-check is used to ensure thread safety. However, when processing a large amount of data, this lock becomes a serious performance bottleneck.

1. Lazy mode of static member instances:

 1   Class  Singleton  2   {  3   Private  :  4       Static Singleton * M_instance; 5   Singleton (){}  6   Public  :  7       Static Singleton * Getinstance ();  8   };  9   10 Singleton * Singleton: getinstance ()  11   {  12      If (Null = M_instance)  13   {  14 Lock (); //  Use other classes for implementation, such as boost  15           If (Null = M_instance)  16   {  17 M_instance = New  Singleton; 18   }  19   Unlock ();  20   }  21       Return  M_instance;  22 }

2. Lazy mode of internal static instances

It should be noted that, after C ++ 0x, the compiler is required to ensure the thread security of internal static variables and can be unlocked. But before C ++ 0x, the lock is still required.

 1   Class  Singletoninside  2   { 3   Private  :  4   Singletoninside (){}  5   Public  :  6       Static Singletoninside * Getinstance ()  7   {  8 Lock (); //  Not needed after C ++ 0x 9           Static  Singletoninside instance;  10 Unlock (); //  Not needed after C ++ 0x  11           Return  Instance;  12   }  13 };

Ii. Hunger mode: whether or not instances of this class are calledProgramAt the beginning, an instance of this class will be generated, and only this instance will be returned later.

The static initialization instance ensures thread security. Why? Because static instance initialization starts at the beginning of the programBefore entering the main function, the main thread completes initialization in a single thread mode.You don't have to worry about multithreading.

Therefore, this mode should be used when the performance requirement is high to avoid frequent lock contention.

 1   Class  Singletonstatic  2   {  3   Private  :  4       Static   Const Singletonstatic * M_instance;  5   Singletonstatic (){}  6   Public :  7       Static Singletonstatic * Getinstance ()  8   {  9           Return  M_instance;  10   }  11   };  12   13   //  Initialize before invoke main externally 14   Const Singletonstatic * singletonstatic: m_instance = New Singletonstatic;

(End)

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.