信号量是CSemaphore的对象,该对象的作用是对访问某个共享资源的线程的数目进行控制。头文件《afxmt.h》
CSemaphore类的构造函数原型如下:
CSemaphore(
LONG lInitialCount /* = 1 */, //计数器的初始值
LONG lMaxCount /* = 1 */, //计数器的最大计数值
LPCTSTR pstrName /* =NULL */, //信号的名称
LPSECURITY_ATTRIBUTES lpsaAttributes /* = NULL */ //指向一个SECURITY_ATTRIBUTES结构的指针
)
LONG lInitialCount /* = 1 */, //计数器的初始值
LONG lMaxCount /* = 1 */, //计数器的最大计数值
LPCTSTR pstrName /* =NULL */, //信号的名称
LPSECURITY_ATTRIBUTES lpsaAttributes /* = NULL */ //指向一个SECURITY_ATTRIBUTES结构的指针
)
信号量对象中有一个可以设置初值的计数器,每当一个线程使用资源时,在该线程中就调用信号计数器对象的成员函数Lock()将该计数器的值减一,当计数器为0时,就不再允许其他线程访问该资源;而当一个线程使用信号量对象的成员函数Unlock()释放资源时,可以将计数器的值加一。因此,信号量对象允许多个线程访问同一个资源,但同时访问该资源的线程总数不能超过信号量对象的最大计数值。
示例:编写一个有四个线程的应用程序,理解信号量对象的使用。
1.创建单文档应用程序;
2.在视图类的实现文件定义一个信号量对象:
CSemaphore semaphoreObj(
2,
3);
3.在视图类的实现文件中定义四个线程函数:
UINT MessageThread1(LPVOID pParam)
{
semaphoreObj.Lock();
LPTSTR pMessage = _T( "Thread1 is started");
CWnd *pMainWnd = AfxGetMainWnd();
::MessageBox(pMainWnd->m_hWnd, pMessage, _T( "Thread messaeg"), MB_OK);
semaphoreObj.Unlock();
return 0;
}
UINT MessageThread2(LPVOID pParam)
{
semaphoreObj.Lock();
LPTSTR pMessage = _T( "Thread2 is started");
CWnd *pMainWnd = AfxGetMainWnd();
::MessageBox(pMainWnd->m_hWnd, pMessage, _T( "Thread messaeg"), MB_OK);
semaphoreObj.Unlock();
return 0;
}
UINT MessageThread3(LPVOID pParam)
{
semaphoreObj.Lock();
LPTSTR pMessage = _T( "Thread3 is started");
CWnd *pMainWnd = AfxGetMainWnd();
::MessageBox(pMainWnd->m_hWnd, pMessage, _T( "Thread messaeg"), MB_OK);
semaphoreObj.Unlock();
return 0;
}
UINT MessageThread4(LPVOID pParam)
{
semaphoreObj.Lock();
LPTSTR pMessage = _T( "Thread4 is started");
CWnd *pMainWnd = AfxGetMainWnd();
::MessageBox(pMainWnd->m_hWnd, pMessage, _T( "Thread messaeg"), MB_OK);
semaphoreObj.Unlock();
return 0;
}
{
semaphoreObj.Lock();
LPTSTR pMessage = _T( "Thread1 is started");
CWnd *pMainWnd = AfxGetMainWnd();
::MessageBox(pMainWnd->m_hWnd, pMessage, _T( "Thread messaeg"), MB_OK);
semaphoreObj.Unlock();
return 0;
}
UINT MessageThread2(LPVOID pParam)
{
semaphoreObj.Lock();
LPTSTR pMessage = _T( "Thread2 is started");
CWnd *pMainWnd = AfxGetMainWnd();
::MessageBox(pMainWnd->m_hWnd, pMessage, _T( "Thread messaeg"), MB_OK);
semaphoreObj.Unlock();
return 0;
}
UINT MessageThread3(LPVOID pParam)
{
semaphoreObj.Lock();
LPTSTR pMessage = _T( "Thread3 is started");
CWnd *pMainWnd = AfxGetMainWnd();
::MessageBox(pMainWnd->m_hWnd, pMessage, _T( "Thread messaeg"), MB_OK);
semaphoreObj.Unlock();
return 0;
}
UINT MessageThread4(LPVOID pParam)
{
semaphoreObj.Lock();
LPTSTR pMessage = _T( "Thread4 is started");
CWnd *pMainWnd = AfxGetMainWnd();
::MessageBox(pMainWnd->m_hWnd, pMessage, _T( "Thread messaeg"), MB_OK);
semaphoreObj.Unlock();
return 0;
}
4.在视图类的鼠标左键消息函数里面添加如下:
void CThreadTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
AfxBeginThread(MessageThread1, _T( "Thread is started"));
AfxBeginThread(MessageThread2, _T( "Thread is started"));
AfxBeginThread(MessageThread3, _T( "Thread is started"));
AfxBeginThread(MessageThread4, _T( "Thread is started"));
CView::OnLButtonDown(nFlags, point);
}
{
AfxBeginThread(MessageThread1, _T( "Thread is started"));
AfxBeginThread(MessageThread2, _T( "Thread is started"));
AfxBeginThread(MessageThread3, _T( "Thread is started"));
AfxBeginThread(MessageThread4, _T( "Thread is started"));
CView::OnLButtonDown(nFlags, point);
}
程序运行结果如下:
参考资料:
1.信号量(CSemaphore)——MFC http://blog.sina.com.cn/s/blog_627ebfc30100iv4w.html
2.《MFC Windows应用程序设计(第2版)》