# 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/>}