// CSemaPhoreDlg 对话框
#define WM_UPDATAEDIT WM_USER+100 //自定义消息用来更新编辑框
HANDLE g_hSemaPhore; //信号对象句柄
HWND g_hwndMain; //当前窗口句柄
//线程函数前置声明
DWORD WINAPI Thread_One(LPVOID lpParam);
DWORD WINAPI Thread_Two(LPVOID lpParam);
DWORD WINAPI Thread_Three(LPVOID lpParam);
DWORD WINAPI Thread_Four(LPVOID lpParam);
DWORD WINAPI Thread_Five(LPVOID lpParam);
DWORD WINAPI Thread_Six(LPVOID lpParam);
DWORD WINAPI Thread_Seven(LPVOID lpParam);
DWORD WINAPI Thread_Eight(LPVOID lpParam);
DWORD WINAPI Thread_Nine(LPVOID lpParam);
DWORD WINAPI Thread_Ten(LPVOID lpParam);
相关全局变量和线程函数的前置声明
afx_msg LRESULT CSemaPhoreDlg::OnUpdataedit(WPARAM wParam, LPARAM lParam)
{
m\_richEdit.UpdateData(TRUE);
CString str;
m\_richEdit.GetWindowText(str);
str += (LPCTSTR)wParam;
str += \_T("\\r");
m\_richEdit.SetWindowText(str);
//开始检测每一行文本,只要有"结束"字符串就把该行用红色显示
CHARFORMAT cf;
ZeroMemory(&cf, sizeof(CHARFORMAT));
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM\_COLOR;
cf.crTextColor = RGB(, , );
CString strLine;
for (int i = ; i<m\_richEdit.GetLineCount(); i++)
{
m\_richEdit.GetLine(i,strLine.GetBufferSetLength(),);
strLine.ReleaseBuffer();
int nIndex = strLine.Find(\_T("结束"));
if(nIndex!=-)
{
int lineStart = m\_richEdit.LineIndex(i);//取当前行第一个字符的索引
int lineEnd = lineStart + nIndex + ;
m\_richEdit.SetSel(lineStart, lineEnd);//选取第一行字符
m\_richEdit.SetSelectionCharFormat(cf);//设置颜色
}
}
m\_richEdit.SetSel(-,-);
m\_richEdit.UpdateData(FALSE);
return ;
}
消息响应_用于更新编辑框信息
BOOL CSemaPhoreDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m\_hIcon, TRUE); // 设置大图标
SetIcon(m\_hIcon, FALSE); // 设置小图标
//创建信号对象,初始和最大使用数量都为10
g\_hSemaPhore = CreateSemaphore(NULL,,,NULL);
//获取主窗口句柄,供线程内部发送消息用以更新编辑框
g\_hwndMain = this->m\_hWnd;
//创建十个员工线程
CloseHandle(CreateThread(NULL,,Thread\_One,NULL,,NULL));
CloseHandle(CreateThread(NULL,,Thread\_Two,NULL,,NULL));
CloseHandle(CreateThread(NULL,,Thread\_Three,NULL,,NULL));
CloseHandle(CreateThread(NULL,,Thread\_Four,NULL,,NULL));
CloseHandle(CreateThread(NULL,,Thread\_Five,NULL,,NULL));
CloseHandle(CreateThread(NULL,,Thread\_Six,NULL,,NULL));
CloseHandle(CreateThread(NULL,,Thread\_Seven,NULL,,NULL));
CloseHandle(CreateThread(NULL,,Thread\_Eight,NULL,,NULL));
CloseHandle(CreateThread(NULL,,Thread\_Nine,NULL,,NULL));
CloseHandle(CreateThread(NULL,,Thread\_Ten,NULL,,NULL));
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
OnInitDialog
//线程_员工1
DWORD WINAPI Thread_One(LPVOID lpParam)
{
WaitForSingleObject(g_hSemaPhore,INFINITE);
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工1:开始上网"),NULL);
//沿时3秒
Sleep();
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工1:上网结束"),NULL);
//增加当前可使用计数
ReleaseSemaphore(g\_hSemaPhore,,NULL);
return TRUE;
}
//线程_员工2
DWORD WINAPI Thread_Two(LPVOID lpParam)
{
WaitForSingleObject(g_hSemaPhore,INFINITE);
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工2:开始上网"),NULL);
//沿时3秒
Sleep();
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工2:上网结束"),NULL);
//增加当前可使用计数
ReleaseSemaphore(g\_hSemaPhore,,NULL);
return TRUE;
}
//线程_员工3
DWORD WINAPI Thread_Three(LPVOID lpParam)
{
WaitForSingleObject(g_hSemaPhore,INFINITE);
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工3:开始上网"),NULL);
//沿时3秒
Sleep();
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工3:上网结束"),NULL);
//增加当前可使用计数
ReleaseSemaphore(g\_hSemaPhore,,NULL);
return TRUE;
}
//线程_员工4
DWORD WINAPI Thread_Four(LPVOID lpParam)
{
WaitForSingleObject(g_hSemaPhore,INFINITE);
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工4:开始上网"),NULL);
//沿时3秒
Sleep();
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工4:上网结束"),NULL);
//增加当前可使用计数
ReleaseSemaphore(g\_hSemaPhore,,NULL);
return TRUE;
}
//线程_员工5
DWORD WINAPI Thread_Five(LPVOID lpParam)
{
WaitForSingleObject(g_hSemaPhore,INFINITE);
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工5:开始上网"),NULL);
//沿时3秒
Sleep();
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工5:上网结束"),NULL);
//增加当前可使用计数
ReleaseSemaphore(g\_hSemaPhore,,NULL);
return TRUE;
}
//线程_员工6
DWORD WINAPI Thread_Six(LPVOID lpParam)
{
WaitForSingleObject(g_hSemaPhore,INFINITE);
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工6:开始上网"),NULL);
//沿时3秒
Sleep();
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工6:上网结束"),NULL);
//增加当前可使用计数
ReleaseSemaphore(g\_hSemaPhore,,NULL);
return TRUE;
}
//线程_员工7
DWORD WINAPI Thread_Seven(LPVOID lpParam)
{
WaitForSingleObject(g_hSemaPhore,INFINITE);
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工7:开始上网"),NULL);
//沿时3秒
Sleep();
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工7:上网结束"),NULL);
//增加当前可使用计数
ReleaseSemaphore(g\_hSemaPhore,,NULL);
return TRUE;
}
//线程_员工8
DWORD WINAPI Thread_Eight(LPVOID lpParam)
{
WaitForSingleObject(g_hSemaPhore,INFINITE);
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工8:开始上网"),NULL);
//沿时3秒
Sleep();
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工8:上网结束"),NULL);
//增加当前可使用计数
ReleaseSemaphore(g\_hSemaPhore,,NULL);
return TRUE;
}
//线程_员工9
DWORD WINAPI Thread_Nine(LPVOID lpParam)
{
WaitForSingleObject(g_hSemaPhore,INFINITE);
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工9:开始上网"),NULL);
//沿时3秒
Sleep();
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工9:上网结束"),NULL);
//增加当前可使用计数
ReleaseSemaphore(g\_hSemaPhore,,NULL);
return TRUE;
}
//线程_员工10
DWORD WINAPI Thread_Ten(LPVOID lpParam)
{
WaitForSingleObject(g_hSemaPhore,INFINITE);
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工10:开始上网"),NULL);
//沿时3秒
Sleep();
SendMessage(g\_hwndMain,WM\_UPDATAEDIT,(WPARAM)\_T("线程\_员工10:上网结束"),NULL);
//增加当前可使用计数
ReleaseSemaphore(g\_hSemaPhore,,NULL);
return TRUE;
}
十个员工线程函数
BOOL CSemaPhoreDlg::DestroyWindow()
{
CloseHandle(g_hSemaPhore);
return CDialogEx::DestroyWindow();
}
DestroyWindow
最大访问数量=3, 当前可访问资源数量=3
线程_员工2:开始上网 //当前可访问资源数量=2
线程_员工1:开始上网 //当前可访问资源数量=1
线程_员工3:开始上网 //当前可访问资源数量=0 为0此时信号量对象为未触发状态,所有等待该信号量的线程都处于等待
线程_员工2:上网结束 //当前可访问资源数量=1 不为0时,此时信号量对象为触发状态,等待的线程中有1个线程可以苏醒
线程_员工3:上网结束 //当前可访问资源数量=2 此时有2个线程可以苏醒
线程_员工1:上网结束 //当前可访问资源数量=3 此时有3个线程可以苏醒
线程_员工4:开始上网 //当前可访问资源数量=2 此时还有2个线程可以苏醒
线程_员工5:开始上网 //当前可访问资源数量=1 此时还有1个线程可以苏醒
线程_员工6:开始上网 //当前可访问资源数量=0 为0此时信号量对象为未触发状态,所有等待该信号量的线程都处于等待
线程_员工4:上网结束 //当前可访问资源数量=1 不为0时,此时信号量对象为触发状态,等待的线程中有1个线程可以苏醒
线程_员工5:上网结束 //当前可访问资源数量=2 此时有2个线程可以苏醒
线程_员工7:开始上网 //当前可访问资源数量=1 此时还有1个线程可以苏醒
线程_员工6:上网结束 //当前可访问资源数量=2 此时有2个线程可以苏醒
线程_员工8:开始上网 //当前可访问资源数量=1 此时还有1个线程可以苏醒
线程_员工9:开始上网 //当前可访问资源数量=0 为0此时信号量对象为未触发状态,所有等待该信号量的线程都处于等待
线程_员工7:上网结束 //当前可访问资源数量=1 不为0时,此时信号量对象为触发状态,等待的线程中有1个线程可以苏醒
线程_员工10:开始上网 //当前可访问资源数量=0 为0此时信号量对象为未触发状态,所有等待该信号量的线程都处于等待
线程_员工8:上网结束 //当前可访问资源数量=1 不为0时,此时信号量对象为触发状态,等待的线程中有1个线程可以苏醒
线程_员工9:上网结束 //当前可访问资源数量=2 此时有2个线程可以苏醒
线程_员工10:上网结束 //当前可访问资源数量=2 此时有3个线程可以苏醒
手机扫一扫
移动阅读更方便
你可能感兴趣的文章