C ++ Single-Column mode and thread security
Generally, the single-column mode in c ++ is easy to implement. We do not need to consider the thread security issue, but we must consider it in a multi-threaded environment. First, let's analyze why this single-column mode is not thread-safe. The common single-column mode is written by copying the code class MsgOfArrival {public :~ MsgOfArrival (void); parameter values; static MsgOfArrival * GetInstance (); private: Encrypted (void); static parameter * m_pInstance;}; MsgOfArrival * MsgOfArrival: m_pInstance = NULL; MsgOfArrival:: MsgOfArrival (void) {} MsgOfArrival ::~ MsgOfArrival (void) {} copies the Code. if two threads execute the GetInstance () function at the same time, when m_pInstance = NULL, two threads enter if at the same time, and two pointers are created, of course, this cannot be done. To solve this problem, we will first think of locking, for example, to copy the code MsgOfArrival * MsgOfArrival: GetInstance () {if (m_pInstance = NULL) {EnterCriticalSection (& g_cs ); m_pInstance = new MsgOfArrival; LeaveCriticalSection (& g_cs)} return m_pInstance;} copy the code. Of course, if m_pInstance is 1 but new is successful, the GetInstance call will not be in if, therefore, a large number of calls to this single column will not cause performance bottlenecks due to the critical section. The second method is to set a new pointer for static variables during initialization, because we know that static variables are initialized by the Main thread before the Main function is executed, since the singleton pointer has been constructed before it enters the main, we certainly do not need to worry about the thread security of the singleton construction in the multi-thread mode after it enters the main. The Code is as follows: copy the code class MsgOfArrival {public :~ MsgOfArrival (void); parameter values; static const MsgOfArrival * GetInstance (); private: MsgOfArrival (void); static const MsgOfArrival * m_pInstance;}; const MsgOfArrival * MsgOfArrival :: m_pInstance = new MsgOfArrival; MsgOfArrival: MsgOfArrival (void) {} MsgOfArrival ::~ MsgOfArrival (void) {} const MsgOfArrival * MsgOfArrival: GetInstance () {return m_pInstance;} note that m_pInstance uses const to modify the copy code, the object is const, Which means we cannot modify this single-column object. This also requires that the function must be const when we call the function to change the single-column function, for example, copy the code int getdata () const; // function declaration // Function Definition int MsgOfArrival: getdata () const {// return m_nVal;} copy the code. Of course, m_pInstance can also be modified without const, however, in this way, its internal data can be modified, which is dangerous when multithreading occurs. The preceding Singleton mode is thread-safe, but there is a problem. How can I release this Singleton after it is created? The first method adds a static function in the class to release the pointer. In this way, we can release the singleton pointer at any time in the program. But sometimes our Singleton pointer will always end with the process, so we can use the second method: copy the code class MsgOfArrival {public :~ MsgOfArrival (void); MAP_STRING_PNORMALMSGCOLLECTION m_normalmsgMap; static const MsgOfArrival * GetInstance (); private: class CGarbo {public :~ CGarbo () {SAFE_DELETE (MsgOfArrival: m_pInstance) ;}}; static CGarbo Garbo; // clear the resource MsgOfArrival (void); static const MsgOfArrival * m_pInstance ;}; copy the code. We added a CGarbo class to the class. This class has only one function to clear single-column object pointers and define a static Garbo object, the object declaration cycle is the same as that of the singleton object pointer. The static Garbo object will be released at the end of the program, and the resources owned by the singleton pointer will also be released. Note that SAFE_DELETE is the release macro defined by myself.