zoukankan      html  css  js  c++  java
  • MFC信号量使用指南

    DEMO程序:TestSemaphore_DEMO.rar

    源代码:TestSemaphore_SRC.rar

    image

    ---关键代码如下---

    void CTestSemaphoreDlg::OnBnClickedButtonThread1()
    {
        // TODO: 在此添加控件通知处理程序代码
        AfxBeginThread((AFX_THREADPROC)thread1WriteA, this);
    }
    
    
    void CTestSemaphoreDlg::OnBnClickedButtonThread2()
    {
        // TODO: 在此添加控件通知处理程序代码
        AfxBeginThread((AFX_THREADPROC)thread2WriteB, this);
    }
    
    
    void CTestSemaphoreDlg::OnBnClickedButtonThread3()
    {
        // TODO: 在此添加控件通知处理程序代码
        AfxBeginThread((AFX_THREADPROC)thread3WriteC, this);
    }
    
    UINT CTestSemaphoreDlg::thread1WriteA( LPVOID pParam )
    {
        CTestSemaphoreDlg* pThis = (CTestSemaphoreDlg*)pParam;
        pThis->WriteA();
        return 0;
    }
    
    void CTestSemaphoreDlg::WriteA()
    {
        CString str;
        g_semaphore.Lock();
        for (int i=0; i<5; i++)
        {
            Sleep(500);
            m_editDisp.GetWindowText(str);
            str += "A";
            m_editDisp.SetWindowText(str);        
        }
        g_semaphore.Unlock();
    }
    
    UINT CTestSemaphoreDlg::thread2WriteB( LPVOID pParam )
    {
        CTestSemaphoreDlg* pThis = (CTestSemaphoreDlg*)pParam;
        pThis->WriteB();
        return 0;
    }
    
    void CTestSemaphoreDlg::WriteB()
    {
        CString str;
        g_semaphore.Lock();
        for (int i=0; i<5; i++)
        {
            Sleep(500);
            m_editDisp.GetWindowText(str);
            str += "B";
            m_editDisp.SetWindowText(str);        
        }
        g_semaphore.Unlock();
    }
    
    UINT CTestSemaphoreDlg::thread3WriteC( LPVOID pParam )
    {
        CTestSemaphoreDlg* pThis = (CTestSemaphoreDlg*)pParam;
        pThis->WriteC();
        return 0;
    }
    
    void CTestSemaphoreDlg::WriteC()
    {
        CString str;
        g_semaphore.Lock();
        for (int i=0; i<5; i++)
        {
            Sleep(500);
            m_editDisp.GetWindowText(str);
            str += "C";
            m_editDisp.SetWindowText(str);        
        }
        g_semaphore.Unlock();
    }

    通过本教程可以迅速学会使用MFC中信号量的运用!

  • 相关阅读:
    python3.5 安装mysqlclient
    python mysqlclient安装失败 Command "python setup.py egg_info" failed with error code 1
    python mysqlclient安装失败 Command "python setup.py egg_info" failed with error code 1
    JUC-多线程锁
    JUC-线程间通信
    JUC-LOCK接口
    JUC-JUC是什么?
    Zookeeper
    Mac 安装IDEA 2018.3 版本
    MyISAM和innoDB
  • 原文地址:https://www.cnblogs.com/zs8861/p/4261318.html
Copyright © 2011-2022 走看看