线程同步:
一:相互排斥同步
  1.原子訪问是windows仅仅有的线程同步
       InterlockedExchange   改变值
       InterlockedIncrement  ++
       InterlockedDecrement  --
       InterlockedExchangeAdd    +=
       实现旋转锁:锁一段代码
       void CThreadOurDlg::OnBnClickedButton1()
       {
        AfxBeginThread(&CThreadOurDlg::ThreadA,this);
        AfxBeginThread(&CThreadOurDlg::ThreadB,this);
       }
       long int g_x=0;
       BOOL flag=FALSE;
       long int a=100;
       long int b=200;
       UINT _cdecl CThreadOurDlg::ThreadA(LPVOID lpvoid)
       {
        while(InterlockedExchange((LONG*)&flag,TRUE)==TRUE)
         Sleep(0);
        a=a+b;
        TRACE("--------------------------------\n");
        TRACE("%d\n",a);
        TRACE("--------------------------------\n");
        InterlockedExchange((LONG*)&flag,FALSE);
        return 0;
       }
        UINT _cdecl CThreadOurDlg::ThreadB(LPVOID lpvoid)
        {
        while(InterlockedExchange((LONG*)&flag,TRUE)==TRUE)
         Sleep(0);
        a=a+b;
        TRACE("++++++++++++++++++++++++++++++++\n");
        TRACE("%d\n",a);
        TRACE("++++++++++++++++++++++++++++++++\n");
        InterlockedExchange((LONG*)&flag,FALSE);
         return 0;
        }
        旋转锁实现卖票:
        long nTicket = 1000;
        BOOL nFlag=FALSE;
        UINT _cdecl CThreadOurDlg::ThreadFun(LPVOID lpvoid)
        {
         int i=(int)lpvoid;
         while(1)
         {
          if(nTicket<=0)
          {
           break;
          }
          if(nTicket>0)
          {
           //------------------------------------------------------------枷锁----------------------------------------
           while(InterlockedExchange((LONG*)&nFlag,TRUE)==TRUE)  
            Sleep(0);
           CString str;
           str.Format(_T("第%d窗体卖出第%d张票"),i,nTicket);
           TRACE("第%d窗体卖出第%d张票\n",i,nTicket);
           InterlockedDecrement(&nTicket);
           //---------------------------------------------------------解锁----------------------------------------------
           InterlockedExchange((LONG*)&nFlag,FALSE);
          }
          Sleep(20);
         }
         TRACE("第%d窗体没有票了\n",i);
         return 0;
        }
        void CThreadOurDlg::OnBnClickedButton2()
        {
         //创建10个工作者线程
         for(int i=0;i<10;i++)
         {
          AfxBeginThread(&CThreadOurDlg::ThreadFun,(LPVOID)i);
         }
        }
  2.临界区
       2.1 Windows的函数
       CRITICAL_SECTION cs;
       ::InitializeCriticalSection(&cs);初始化临界区
       ::InitializeCriticalSectionAndSpinCount(&cs,4000);包括旋转锁
       ::DeleteCriticalSection(&cs);销毁
       ::TryEnterCriticalSection尝试进入临界区
       ::EnterCriticalSection(&((CThreadOurDlg*)AfxGetApp()->m_pMainWnd)->cs);进入
       ::LeaveCriticalSection(&((CThreadOurDlg*)AfxGetApp()->m_pMainWnd)->cs);退出
       2.2 MFC的函数
       CCriticalSection o_cs;
       o_cs.Lock();
       o_cs.Unlock();
  3.相互排斥量  内核模式  有系统管理
      3.1 MFC的类
      void CThreadOurDlg::OnBnClickedButton2()
       {
        //创建10个工作者线程
        for(int i=0;i<10;i++)
        {
         AfxBeginThread(&CThreadOurDlg::ThreadFun,(LPVOID)i);
        }
        //释放相互排斥量
        ::ReleaseMutex(mx.m_hObject);
       }
       UINT _cdecl CThreadOurDlg::ThreadFun(LPVOID lpvoid)
       {
        int i=(int)lpvoid;
        while(1)
        {
         if(nTicket<=0)
         {
          break;
         }
         //----------------------------------等待相互排斥量信号------------------------------------
         ::WaitForSingleObject(((CThreadOurDlg*)AfxGetApp()->m_pMainWnd)->mx.m_hObject,INFINITE);
         if(nTicket>0)
         {
          CString str;
          str.Format(_T("第%d窗体卖出第%d张票"),i,nTicket);
          TRACE("第%d窗体卖出第%d张票\n",i,nTicket);
          ((CThreadOurDlg*)AfxGetApp()->m_pMainWnd)->m_listBox.AddString(str);
          InterlockedDecrement(&nTicket);
         }
         //释放相互排斥量
         ::ReleaseMutex(((CThreadOurDlg*)AfxGetApp()->m_pMainWnd)->mx.m_hObject);
         Sleep(20);
        }
        TRACE("第%d窗体没有票了\n",i);
        return 0;
       }
       3.2 HANDLE m_hMutex ;
       //创建一个相互排斥量
       m_hMutex=CreateMutex(NULL,FALSE,NULL);
       CloseHandle(m_hMutex);
       m_hMutex=NULL; 

 4.事件
      三个进度条一起跑
      4.1MFC类   CEvent cc(FALSE,TRUE);人工
      4.2Windows   HANDLE m_hEvent;
      ::WaitForSingleObject(m_hEvent,INFINITE);
  5.信号量
    5.1Windows    HANDLE m_hSingle;
             m_hSingle = ::CreateSemaphore(NULL,0/*初始值有几个信号*/,3/*最多能释放几个信号*/,NULL);
          ::RealeaseSemaphore(m_hSingle,3/*一次释放多少个*/,NULL/*上一次释放多少个*/);
    5.2MFC CSemaphore


 

posted on 2017-07-22 11:03  yutingliuyl  阅读(129)  评论(0编辑  收藏  举报