Test code:
1 //the threading model can start as a member variable of a class2 HANDLE M_hlogthread;3 CEVT m_evloglist;4 ccrisec m_csloglist;5 //waiting for thread messages6 voidWaitforwritelogthreadcomplete ()7 {8 M_evloglist.set ();9VERIFY (:: WaitForSingleObject (M_hlogthread, INFINITE) = =wait_object_0);Ten One :: CloseHandle (M_hlogthread); AM_hlogthread =NULL; - } - the //thread function Body - - UINT WINAPI logthreadfunc (LPVOID PV) - { +ct1dlg* pThis = (ct1dlg*) PV; - ASSERT (pThis); + Amylist<int>lsloglist; at - while(TRUE) - { -VERIFY (:: WaitForSingleObject (m_evloglist, INFINITE) = =wait_object_0); - { - ccriseclock LocalLock (m_csloglist); in - if(Pthis->m_ndatalistforthread! =0) to { +TRACE ("Front: mlist_size=%d%d\n",pthis->m_lsloglist.size (), lsloglist.size ()); -Pthis->M_lsloglist.swap (lsloglist); theTRACE ("after: mlist_size=%d%d\n",pthis->m_lsloglist.size (), lsloglist.size ()); * } $ ElsePanax Notoginseng { -Pthis->m_lsloglist.clear (); the return 0; + } A } the + - $ while(Lsloglist.size () >0) $ { - intITemp =Lsloglist.front (); -TRACE ("itemp=%x%d\n", GetCurrentThreadID (), iTemp); the Lsloglist.pop_front (); - }Wuyi the //destroying a file handle - } Wu - return 0; About } $ - - - A + voidCt1dlg::onbutton15 ()//Initializing Threads the { - //Todo:add your control notification handler code here $ //construct Init theM_hlogthread=NULL; theM_ndatalistforthread=1; the //End construct Init theM_hlogthread = (HANDLE) _beginthreadex (NULL,0, Logthreadfunc, (LPVOID) This,0, NULL); - in if(!m_hlogthread) the { theTRACE ("Thread creation failed! \ n"); About return ; the } the the } + - voidCt1dlg::onbutton17 ()//Destroying Threads the {Bayi //Todo:add your control notification handler code here the theM_ndatalistforthread =0;//None - if(M_hlogthread) - { the Waitforwritelogthreadcomplete (); the } the the } - the voidCt1dlg::onbutton16 ()//thread-driven requires a thread to do something that is time-consuming or does not allow the main card to die the { the //Todo:add your control notification handler code here94 Static intCnt=0; the { the ccriseclock LocalLock (m_csloglist); theM_lsloglist.push_back (+ +)CNT);98M_lsloglist.push_back (+ +)CNT); About if(cnt>Ten) CNT =cnt%Ten; - }101 102 M_evloglist.set ();103}
Public code: Thank CNBLOGSCOM/LDCSAA
CEVT:
1 classCEVT2 {3 Public:4CEVT (bool bManualReset = FALSE, bool Binitialstate = False, LPCTSTR pszname = NULL, lpsecurity_attributes psecurity =NULL)5 {6M_hevent =:: CreateEvent (Psecurity, bManualReset, Binitialstate, pszname);7 ASSERT (IsValid ());8 }9 Ten~CEVT () One { A if(IsValid ()) - VERIFY (:: CloseHandle (M_hevent)); - } the - bool Open (DWORD dwaccess, bool bInheritHandle, LPCTSTR pszname) - { - if(IsValid ()) + VERIFY (:: CloseHandle (M_hevent)); - +M_hevent =:: OpenEvent (Dwaccess, bInheritHandle, pszname); A return(IsValid ()); at } - -BOOL Pulse () {return(::P ulseevent (M_hevent));} -BOOL Reset () {return(:: ResetEvent (M_hevent));} -BOOL Set () {return(:: SetEvent (M_hevent));} - inhandle& GetHandle () {returnm_hevent;} - operatorHANDLE () {returnm_hevent;} toBOOL IsValid () {returnM_hevent! =NULL;} + - Private: theCEVT (Constcevt&); *CEvtoperator= (Constcevt&); $ Panax Notoginseng Private: - HANDLE m_hevent; the};
Ccrisec:
1 classccrisec2 {3 Public:4Ccrisec () {:: InitializeCriticalSection (&m_crisec);}5~ccrisec () {::D eletecriticalsection (&m_crisec);}6 7 voidLock () {:: EnterCriticalSection (&m_crisec);}8 voidUnlock () {:: LeaveCriticalSection (&m_crisec);}9 Ten Private: OneCcrisec (Constccrisec&CS); ACcrisecoperator= (Constccrisec&CS); - - Private: the critical_section m_crisec; -};
Finish. boyang987
VC threading Model initialization, destruction, drive, event and critical section