• 线程同步


    线程同步:
    一:相互排斥同步
      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("--------------------------------
    ");
            TRACE("%d
    ",a);
            TRACE("--------------------------------
    ");
            InterlockedExchange((LONG*)&flag,FALSE);
            return 0;
           }
            UINT _cdecl CThreadOurDlg::ThreadB(LPVOID lpvoid)
            {
            while(InterlockedExchange((LONG*)&flag,TRUE)==TRUE)
             Sleep(0);
            a=a+b;
            TRACE("++++++++++++++++++++++++++++++++
    ");
            TRACE("%d
    ",a);
            TRACE("++++++++++++++++++++++++++++++++
    ");
            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张票
    ",i,nTicket);
               InterlockedDecrement(&nTicket);
               //---------------------------------------------------------解锁----------------------------------------------
               InterlockedExchange((LONG*)&nFlag,FALSE);
              }
              Sleep(20);
             }
             TRACE("第%d窗体没有票了
    ",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张票
    ",i,nTicket);
              ((CThreadOurDlg*)AfxGetApp()->m_pMainWnd)->m_listBox.AddString(str);
              InterlockedDecrement(&nTicket);
             }
             //释放相互排斥量
             ::ReleaseMutex(((CThreadOurDlg*)AfxGetApp()->m_pMainWnd)->mx.m_hObject);
             Sleep(20);
            }
            TRACE("第%d窗体没有票了
    ",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
    


     

  • 相关阅读:
    做了点数论的题
    2016日报总结
    2016在H3C
    2017春节小目标
    2016项目小总结
    明明的随机数
    计算字符个数
    字符串最后一个单词的长度
    字符串分隔
    考研回忆
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7220652.html
Copyright © 2020-2023  润新知