VC 线程模型 初始化 、销毁 、驱动,事件与临界区

测试代码:

  1 //线程模型开始,都可以作为类的成员变量
  2 HANDLE                m_hLogThread;
  3 CEvt                m_evLogList;
  4 CCriSec                m_csLogList;
  5 //等待线程消息
  6 void WaitForWriteLogThreadComplete()
  7 {
  8     m_evLogList.Set();
  9     VERIFY(::WaitForSingleObject(m_hLogThread, INFINITE) == WAIT_OBJECT_0);
 10     
 11     ::CloseHandle(m_hLogThread);
 12     m_hLogThread    = NULL;
 13 }
 14 
 15 //线程函数体
 16 
 17 UINT WINAPI LogThreadFunc(LPVOID pv)
 18 {
 19     CT1Dlg* pThis    = (CT1Dlg*)pv;
 20     ASSERT(pThis);
 21     
 22     MYList<int > lsLogList;
 23     
 24     while(TRUE)
 25     {
 26         VERIFY(::WaitForSingleObject(m_evLogList, INFINITE) == WAIT_OBJECT_0);
 27         {
 28             CCriSecLock locallock(m_csLogList);
 29             
 30             if(pThis->m_nDataListForThread != 0)
 31             {
 32                 TRACE("前:mList_size=%d %d\n",pThis->m_lsLogList.size(),lsLogList.size());
 33                 pThis->m_lsLogList.swap(lsLogList);
 34                 TRACE("后:mList_size=%d %d\n",pThis->m_lsLogList.size(),lsLogList.size());
 35             }
 36             else
 37             {
 38                 pThis->m_lsLogList.clear();
 39                 return 0;
 40             }
 41         }
 42 
 43     
 44         
 45         while(lsLogList.size() > 0)
 46         {
 47             int iTemp = lsLogList.front();
 48             TRACE("iTemp=%X %d\n",GetCurrentThreadId(),iTemp);    
 49             lsLogList.pop_front();
 50         }
 51         
 52         //销毁文件句柄
 53     }
 54 
 55     return 0;
 56 }
 57 
 58 
 59 
 60 
 61 
 62 void CT1Dlg::OnButton15() //初始化线程
 63 {
 64     // TODO: Add your control notification handler code here
 65     //construct init
 66         m_hLogThread= NULL;
 67         m_nDataListForThread=1;
 68     //end construct init
 69     m_hLogThread = (HANDLE)_beginthreadex(NULL, 0, LogThreadFunc, (LPVOID)this, 0, NULL);
 70     
 71     if(!m_hLogThread)
 72     {
 73         TRACE("线程创建失败!\n");
 74         return ;
 75     }
 76     
 77 }
 78 
 79 void CT1Dlg::OnButton17() //销毁线程
 80 {
 81     // TODO: Add your control notification handler code here
 82 
 83     m_nDataListForThread =0;//none
 84     if(m_hLogThread)
 85     {
 86         WaitForWriteLogThreadComplete();
 87     }
 88  
 89 }
 90 
 91 void CT1Dlg::OnButton16() //线程驱动 需要线程做某件耗时或不能让主线程卡死的事
 92 {
 93     // TODO: Add your control notification handler code here
 94     static int cnt=0;
 95     {
 96         CCriSecLock locallock(m_csLogList);
 97         m_lsLogList.push_back(++cnt);
 98         m_lsLogList.push_back(++cnt);
 99         if(cnt>10) cnt =cnt %10;
100     }
101     
102     m_evLogList.Set();
103 }

公共代码:感谢 cnblogscom/ldcsaa

CEvt:

 1 class CEvt
 2 {
 3 public:
 4     CEvt(BOOL bManualReset = FALSE, BOOL bInitialState = FALSE, LPCTSTR pszName = NULL, LPSECURITY_ATTRIBUTES pSecurity = NULL)
 5     {
 6         m_hEvent = ::CreateEvent(pSecurity, bManualReset, bInitialState, pszName);
 7         ASSERT(IsValid());
 8     }
 9 
10     ~CEvt()
11     {
12         if(IsValid())
13             VERIFY(::CloseHandle(m_hEvent));
14     }
15 
16     BOOL Open(DWORD dwAccess, BOOL bInheritHandle, LPCTSTR pszName)
17     {
18         if(IsValid())
19             VERIFY(::CloseHandle(m_hEvent));
20 
21         m_hEvent = ::OpenEvent(dwAccess, bInheritHandle, pszName);
22         return(IsValid());
23     }
24 
25     BOOL Pulse()    {return(::PulseEvent(m_hEvent));}
26     BOOL Reset()    {return(::ResetEvent(m_hEvent));}
27     BOOL Set()        {return(::SetEvent(m_hEvent));}
28 
29     HANDLE& GetHandle    ()     {return m_hEvent;}
30     operator HANDLE        ()    {return m_hEvent;}
31     BOOL IsValid        ()    {return m_hEvent != NULL;}
32 
33 private:
34     CEvt(const CEvt&);
35     CEvt operator = (const CEvt&);
36 
37 private:
38     HANDLE m_hEvent;
39 };

CCriSec:

 1 class CCriSec
 2 {
 3 public:
 4     CCriSec()        {::InitializeCriticalSection(&m_crisec);}
 5     ~CCriSec()        {::DeleteCriticalSection(&m_crisec);}
 6 
 7     void Lock()        {::EnterCriticalSection(&m_crisec);}
 8     void Unlock()    {::LeaveCriticalSection(&m_crisec);}
 9 
10 private:
11     CCriSec(const CCriSec& cs);
12     CCriSec operator = (const CCriSec& cs);
13 
14 private:
15     CRITICAL_SECTION    m_crisec;
16 };

完。boyang987

 

posted @ 2015-04-01 11:08  boyang987  阅读(278)  评论(0编辑  收藏  举报