Share a thread-safe single-Instance template class

Source: Internet
Author: User
Tags volatile

Singleton mode should be the simplest design mode. Share a thread-safe, single-instance template class here.

Template <typename type>class csingleton{public:static type* getinstance () {// Kbeingcreatedmarker is used to indicate that a singleton instance is in the process of being created. This is initialized to 1 because the operating system does not assign a pointer with an address of 1. Static const volatile intptr_t Kbeingcreatedmarker = 1;//returns M_pinstanceif if M_pinstance is not empty and is not being created (m_pinstance! = NULL & amp;& m_pinstance! = kbeingcreatedmarker) {return reinterpret_cast<type*> (m_pinstance);} Use the InterlockedCompareExchange function to ensure that the atomic operation//function determines whether m_pinstance is equal to NULL, and if so, assigns the m_pinstance value to kbeingcreatedmarker// The function returns the initial value of m_pinstance by determining whether the return value is equal to NULL to know if it can be instantiated if (InterlockedCompareExchange (Reinterpret_cast<volatile LONG *> (&m_pinstance),static_cast<long> (kbeingcreatedmarker),static_cast<long> (null)) = = NULL) { static Type newval;m_pinstance = Reinterpret_cast<intptr_t> (&newval); return &newval;} If M_pinstance is Kbeingcreatedmarker, it is being created in//SwitchToThread Let the remaining time slices wait for the creation process to complete while (m_pinstance = = Kbeingcreatedmarker) {switchtothread ();} Arriving here indicates that the creation process has completed the return reinterpret_cast<type*> (M_pinstanCE);} type& operator* () {return *getinstance ();} type* operator-> () {return getinstance ();} private:static volatile intptr_t m_pinstance;}; Template <typename type>volatile intptr_t csingleton<type>::m_pinstance = NULL;

Let's say we have a Ccmdmanager class.

Class Ccmdmanager{public:ccmdmanager () {std::cout << "Hello, I am the only one!";};};

The method of use is simple, as follows:

int main () {Ccmdmanager *pmgr = csingleton<ccmdmanager>::getinstance (); Ccmdmanager &mgr = * (Csingleton<ccmdmanager>::getinstance ());}

If we want to completely restrict ccmdmanager from being instantiated for the second time, we can do this

Class Ccmdmanager{private:ccmdmanager () {std::cout << "Hello, I am the only one!";}; Friend Class csingleton<ccmdmanager>;};

By setting the constructor to private and opening only to class csingleton<ccmdmanager>, you can guarantee that the user can only use Ccmdmanager *pmgr = csingleton< Ccmdmanager>::getinstance () is called in this way.

By the way, some of the limitations of this Code

1. Because the function of InterlockedCompareExchange is used, it can only be used under Windows, but it is undeniable that the function is extremely efficient, and the comparison is done and exchanged as long as one instruction.

2. Csingleton only the default constructors are supported when instantiating an object. In general, this is acceptable.

Share a thread-safe single-Instance template class

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.