加库:
#include <afxmt.h>
申明全局:
UINT SemaphoreThread1(LPVOID pParam);
UINT SemaphoreThread2(LPVOID pParam);
UINT SemaphoreThread3(LPVOID pParam);
UINT SemaphoreThread2(LPVOID pParam);
UINT SemaphoreThread3(LPVOID pParam);
CSemaphore m_Semaphore(2, 2, NULL);
int g_sharedata = 0;
int g_sharedata = 0;
定义3个按钮:
void CSemaphoreDlg::OnRun1()
{
// TODO: Add your control notification handler code here
//获取窗口句柄
HWND hWnd = GetSafeHwnd();
//产生新的线程
AfxBeginThread(SemaphoreThread1, hWnd);
}
{
// TODO: Add your control notification handler code here
//获取窗口句柄
HWND hWnd = GetSafeHwnd();
//产生新的线程
AfxBeginThread(SemaphoreThread1, hWnd);
}
void CSemaphoreDlg::OnRun2()
{
// TODO: Add your control notification handler code here
//获取窗口句柄
HWND hWnd = GetSafeHwnd();
//产生新的线程
AfxBeginThread(SemaphoreThread2, hWnd);
}
{
// TODO: Add your control notification handler code here
//获取窗口句柄
HWND hWnd = GetSafeHwnd();
//产生新的线程
AfxBeginThread(SemaphoreThread2, hWnd);
}
void CSemaphoreDlg::OnRun3()
{
// TODO: Add your control notification handler code here
//获取窗口句柄
HWND hWnd = GetSafeHwnd();
//产生新的线程
AfxBeginThread(SemaphoreThread3, hWnd);
}
{
// TODO: Add your control notification handler code here
//获取窗口句柄
HWND hWnd = GetSafeHwnd();
//产生新的线程
AfxBeginThread(SemaphoreThread3, hWnd);
}
定义3个工作线程:
UINT SemaphoreThread1(LPVOID pParam)
{
HWND hWnd = (HWND)pParam;
while(1)
{
CString str;
{
HWND hWnd = (HWND)pParam;
while(1)
{
CString str;
m_Semaphore.Lock();
str = "锁定";
::SetDlgItemText((HWND)pParam, IDC_EDITTEXT1, str);
::Sleep(1000);
str = "锁定";
::SetDlgItemText((HWND)pParam, IDC_EDITTEXT1, str);
::Sleep(1000);
g_sharedata++;
str.Format("%d", g_sharedata);
::SetDlgItemText((HWND)pParam, IDC_EDIT1, str);
str.Format("%d", g_sharedata);
::SetDlgItemText((HWND)pParam, IDC_EDIT1, str);
::Sleep(1000);
m_Semaphore.Unlock();
m_Semaphore.Unlock();
str = "解锁";
::SetDlgItemText((HWND)pParam, IDC_EDITTEXT1, str);
::Sleep(1000);
::SetDlgItemText((HWND)pParam, IDC_EDITTEXT1, str);
::Sleep(1000);
}
return 0;
}
}
UINT SemaphoreThread2(LPVOID pParam)
{
HWND hWnd = (HWND)pParam;
while(1)
{
CString str;
m_Semaphore.Lock();
str = "锁定";
::SetDlgItemText((HWND)pParam, IDC_EDITTEXT2, str);
::Sleep(1000);
str.Format("%d", g_sharedata);
::SetDlgItemText((HWND)pParam, IDC_EDIT2, str);
{
HWND hWnd = (HWND)pParam;
while(1)
{
CString str;
m_Semaphore.Lock();
str = "锁定";
::SetDlgItemText((HWND)pParam, IDC_EDITTEXT2, str);
::Sleep(1000);
str.Format("%d", g_sharedata);
::SetDlgItemText((HWND)pParam, IDC_EDIT2, str);
::Sleep(1000);
m_Semaphore.Unlock();
str = "解锁";
::SetDlgItemText((HWND)pParam, IDC_EDITTEXT2, str);
::Sleep(1000);
m_Semaphore.Unlock();
str = "解锁";
::SetDlgItemText((HWND)pParam, IDC_EDITTEXT2, str);
::Sleep(1000);
}
return 0;
}
}
UINT SemaphoreThread3(LPVOID pParam)
{
HWND hWnd = (HWND)pParam;
while(1)
{
CString str;
{
HWND hWnd = (HWND)pParam;
while(1)
{
CString str;
m_Semaphore.Lock();
str = "锁定";
::SetDlgItemText((HWND)pParam, IDC_EDITTEXT3, str);
::Sleep(1000);
str = "锁定";
::SetDlgItemText((HWND)pParam, IDC_EDITTEXT3, str);
::Sleep(1000);
str.Format("%d", g_sharedata);
::SetDlgItemText((HWND)pParam, IDC_EDIT3, str);
::SetDlgItemText((HWND)pParam, IDC_EDIT3, str);
::Sleep(1000);
m_Semaphore.Unlock();
str = "解锁";
::SetDlgItemText((HWND)pParam, IDC_EDITTEXT3, str);
::Sleep(1000);
m_Semaphore.Unlock();
str = "解锁";
::SetDlgItemText((HWND)pParam, IDC_EDITTEXT3, str);
::Sleep(1000);
}
return 0;
}
}