码迷,mamicode.com
首页 > 编程语言 > 详细

线程同步

时间:2017-07-22 12:16:02      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:ack   leave   工作   creat   while   一个   UI   bre   系统   

线程同步:
一:相互排斥同步
  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


 

线程同步

标签:ack   leave   工作   creat   while   一个   UI   bre   系统   

原文地址:http://www.cnblogs.com/yutingliuyl/p/7220652.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!