void  CMultiThread_MutexDlg::TestThreadMutex()
{
   // CMutex g_clsMutex(FALSE,NULL);
//    g_clsMutex=CMutex(FALSE,NULL);
     theApp.g_clsMutex=new CMutex(false,_T("abc"));
    AfxBeginThread(ThreadProc27,NULL);

    AfxBeginThread(ThreadProc28,NULL);
    Sleep(300);
    CString  sResult=CString(theApp.g_cArray);
    int  ll=sResult.GetLength();
    AfxMessageBox(sResult);
    delete theApp.g_clsMutex;
}
UINT  CMultiThread_MutexDlg::ThreadProc27(LPVOID pParam)
{    
  theApp.g_clsMutex->Lock();
    for(int i=1;i<10;i++)
    {
         theApp.g_cArray[i]='a';
         Sleep(1);
    }
     theApp.g_clsMutex->Unlock();
     
    return 0;
}
UINT CMultiThread_MutexDlg::ThreadProc28(LPVOID pParam)
{
     theApp.g_clsMutex->Lock();
    for(int i=0;i<1;i++)
    {
         theApp.g_cArray[i]='b';
         Sleep(1);
    }
     theApp.g_clsMutex->Unlock();
    return 0;
}

全局变量

class CMultiThread_MutexApp : public CWinApp
{
public:
    CMultiThread_MutexApp();

// 重写
public:
    CMutex *g_clsMutex;
    char g_cArray[10];
    virtual BOOL InitInstance();

// 实现

     DECLARE_MESSAGE_MAP()
};

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-22
  • 2021-09-27
  • 2021-08-25
  • 2021-09-08
  • 2022-02-03
猜你喜欢
  • 2022-12-23
  • 2022-01-09
  • 2022-12-23
  • 2022-12-23
  • 2021-06-29
  • 2022-12-23
  • 2021-08-26
相关资源
相似解决方案