Implementation of a thread-safe Singleton Mode

Source: Internet
Author: User
# Include <iostream> <br/> # include <vector> <br/> # include <bitset> <br/> # include <assert. h> <br/> # include <windows. h> <br/> # include <process. h> </P> <p> using namespace STD; </P> <p> class csingleton <br/>{< br/> PRIVATE: <br/> class cassistforsingleton <br/> {<br/> PRIVATE: <br/> critical_section m_cs; </P> <p> Public: <br/> cassistforsingleton () <br/>{< br/> initializecriticalsection (& m_cs); <br/>}</P> <P> ~ Cassistforsingleton () <br/>{< br/> deletecriticalsection (& m_cs); <br/>}</P> <p> Public: <br/> void lock () <br/>{< br/> entercriticalsection (& m_cs); <br/>}</P> <p> void unlock () <br/>{< br/> leavecriticalsection (& m_cs); <br/>}< br/>}; </P> <p> PRIVATE: <br/> static cassistforsingleton m_refsycobj; <br/> static csingleton * m_pinstance; </P> <p> static int m_ndata; </P> <p> PRIVATE: <br/> csingleton () <br />{</P> <p >}</P> <p> Public: <br/> static csingleton * getinstatnce () <br/>{< br/> m_refsycobj.lock (); <br/> If (null = m_pinstance) <br/>{< br/> m_pinstance = new csingleton; <br/> cout <"New csingleton" <Endl; <br/>}< br/> m_refsycobj.unlock (); </P> <p> return m_pinstance; <br/>}</P> <p> Public: <br/> static int getdata () <br/>{< br/> return m_ndata; <br/>}</P> <p> static void setdata (INT ndata) <Br/>{< br/> m_refsycobj.lock (); <br/> m_ndata = ndata; <br/> m_refsycobj.unlock (); <br/>}< br/>}; </P> <p> csingleton: cassistforsingleton csingleton: m_refsycobj = csingleton: cassistforsingleton (); <br/> csingleton * csingleton: m_pinstance = NULL; <br/> int csingleton: m_ndata = 0; </P> <p> unsigned int winapi threadfun (void *) <br/> {<br/> cout <"launcher thread" <Endl; </P> <p> for (INT I = 0; I <99999999; I ++) <br/>{< br/> csingleton * psingl = csingleton: getinstatnce (); <br/> If (null! = Psingl) <br/> {<br/> psingl-> setdata (I); <br/>}</P> <p> sleep (500 ); <br/>}</P> <p> return 0; <br/>}</P> <p> int main (INT argv, char * argc []) <br/>{< br/> uintptr_t handlethread [10]; <br/> unsigned int nthreadid = 0; <br/> for (INT I = 0; I <10; I ++) <br/>{< br/> handlethread [I] = _ beginthreadex (null, 0, threadfun, null, 0, & nthreadid ); <br/>}</P> <p> waitformultipleobjects (10, (const handle *) handlethread, true, infinite); </P> <p> return 0; <br/>}

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: 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.