标签:必须 添加 共享资源 死锁 bbb eve wait while 启动
1 int g_nTickNum = 10; //总票数 2 CEdit* g_editShowInfo; //编辑框控件指针 3 CRITICAL_SECTION g_cs; //临界区对象 4 5 //线程函数前置声明 6 DWORD WINAPI ThreadSellTicket_One(LPVOID lpParam); 7 DWORD WINAPI ThreadSellTicket_Two(LPVOID lpParam); 8 DWORD WINAPI ThreadSellTicket_Three(LPVOID lpParam);
1 BOOL CCriticalSectionDemoDlg::OnInitDialog() 2 { 3 CDialogEx::OnInitDialog(); 4 5 // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动 6 // 执行此操作 7 SetIcon(m_hIcon, TRUE); // 设置大图标 8 SetIcon(m_hIcon, FALSE); // 设置小图标 9 10 //初始化临界区 11 InitializeCriticalSection(&g_cs); 12 //获取EDIT控件指针,供线程内部访问 13 g_editShowInfo = (CEdit*)GetDlgItem(IDC_EDIT_SHOWINFO); 14 15 return TRUE; // 除非将焦点设置到控件,否则返回 TRUE 16 }
1 void CCriticalSectionDemoDlg::OnBnClickedBtnSellticket() 2 { 3 //创建三个售票窗口线程,创建后直接关闭句柄 4 CloseHandle(CreateThread(NULL,0,ThreadSellTicket_One,NULL,0,NULL)); 5 CloseHandle(CreateThread(NULL,0,ThreadSellTicket_Two,NULL,0,NULL)); 6 CloseHandle(CreateThread(NULL,0,ThreadSellTicket_Three,NULL,0,NULL)); 7 }
1 //线程_售票窗口1 2 DWORD WINAPI ThreadSellTicket_One(LPVOID lpParam) 3 { 4 CString strEdit; 5 CString strNew; 6 while (true) 7 { 8 Sleep(1000); 9 EnterCriticalSection(&g_cs); 10 g_editShowInfo->GetWindowText(strEdit); 11 if(g_nTickNum > 0) 12 { 13 strNew.Format(_T("线程1:剩余票数:%d,售出1张票\r\n"),g_nTickNum--); 14 strEdit += strNew; 15 if(g_nTickNum == 0) 16 { 17 strEdit += _T("线程1:票己售空,关闭售票窗口\r\n"); 18 g_editShowInfo->SetWindowText(strEdit); 19 LeaveCriticalSection(&g_cs); 20 break; 21 } 22 else 23 { 24 g_editShowInfo->SetWindowText(strEdit); 25 LeaveCriticalSection(&g_cs); 26 } 27 } 28 else 29 { 30 strEdit += _T("线程1:票己售空,关闭售票窗口\r\n"); 31 g_editShowInfo->SetWindowTextW(strEdit); 32 LeaveCriticalSection(&g_cs); 33 break; 34 } 35 } 36 return true; 37 }
1 DWORD WINAPI ThreadSellTicket_Two(LPVOID lpParam) 2 { 3 CString strEdit; 4 CString strNew; 5 while (true) 6 { 7 Sleep(1000); 8 EnterCriticalSection(&g_cs); 9 g_editShowInfo->GetWindowText(strEdit); 10 if(g_nTickNum > 0) 11 { 12 strNew.Format(_T("线程2:剩余票数:%d,售出1张票\r\n"),g_nTickNum--); 13 strEdit += strNew; 14 if(g_nTickNum == 0) 15 { 16 strEdit += _T("线程2:票己售空,关闭售票窗口\r\n"); 17 g_editShowInfo->SetWindowText(strEdit); 18 LeaveCriticalSection(&g_cs); 19 break; 20 } 21 else 22 { 23 g_editShowInfo->SetWindowText(strEdit); 24 LeaveCriticalSection(&g_cs); 25 } 26 } 27 else 28 { 29 strEdit += _T("线程2:票己售空,关闭售票窗口\r\n"); 30 g_editShowInfo->SetWindowTextW(strEdit); 31 LeaveCriticalSection(&g_cs); 32 break; 33 } 34 } 35 return true; 36 }
1 DWORD WINAPI ThreadSellTicket_Three(LPVOID lpParam) 2 { 3 CString strEdit; 4 CString strNew; 5 while (true) 6 { 7 Sleep(1000); 8 EnterCriticalSection(&g_cs); 9 g_editShowInfo->GetWindowText(strEdit); 10 if(g_nTickNum > 0) 11 { 12 strNew.Format(_T("线程3:剩余票数:%d,售出1张票\r\n"),g_nTickNum--); 13 strEdit += strNew; 14 if(g_nTickNum == 0) 15 { 16 strEdit += _T("线程3:票己售空,关闭售票窗口\r\n"); 17 g_editShowInfo->SetWindowText(strEdit); 18 LeaveCriticalSection(&g_cs); 19 break; 20 } 21 else 22 { 23 g_editShowInfo->SetWindowText(strEdit); 24 LeaveCriticalSection(&g_cs); 25 } 26 } 27 else 28 { 29 strEdit += _T("线程3:票己售空,关闭售票窗口\r\n"); 30 g_editShowInfo->SetWindowTextW(strEdit); 31 LeaveCriticalSection(&g_cs); 32 break; 33 } 34 } 35 return true; 36 }
1 BOOL CCriticalSectionDemoDlg::DestroyWindow() 2 { 3 //删除临界区 4 DeleteCriticalSection(&g_cs); 5 return CDialogEx::DestroyWindow(); 6 }
多线程(四)多线程同步_Critical Section临界区
标签:必须 添加 共享资源 死锁 bbb eve wait while 启动
原文地址:https://www.cnblogs.com/fzxiaoyi/p/12072551.html