函数CreateSemaphore和ReleaseSemaphore
来源:互联网 发布:淘宝店铺优惠券平台 编辑:程序博客网 时间:2024/05/22 12:24
函数CreateSemaphore和ReleaseSemaphore声明如下:
WINBASEAPI
__out
HANDLE
WINAPI
CreateSemaphoreA(
__in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
__in LONG lInitialCount,
__in LONG lMaximumCount,
__in_opt LPCSTR lpName
);
WINBASEAPI
__out
HANDLE
WINAPI
CreateSemaphoreW(
__in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
__in LONG lInitialCount,
__in LONG lMaximumCount,
__in_opt LPCWSTR lpName
);
#ifdef UNICODE
#define CreateSemaphore CreateSemaphoreW
#else
#define CreateSemaphore CreateSemaphoreA
#endif // !UNICODE
lpSemaphoreAttributes是信号量的安全属性。
lInitialCount是初始化的信号量。
lMaximumCount是允许信号量增加到最大值。
lpName是信号量的名称。
WINAPI
ReleaseSemaphore(
__in HANDLE hSemaphore,
__in LONG lReleaseCount,
__out_opt LPLONG lpPreviousCount
);
hSemaphore是要增加的信号量句柄。
lReleaseCount是增加的计数。
lpPreviousCount是增加前的数值返回。
调用函数的例子如下:
#001 //线程运行函数。
#002 //在这里可以使用类里的成员,也可以让派生类实现更强大的功能。
#004 DWORD CThreadSemaphore::Run(void)
#005 {
#006 //输出到调试窗口。
#007 ::OutputDebugString(_T("Run()线程函数运行/r/n"));
#008
#009 //
#010 const LONG cMax = 10;
#011 m_hSemaphore = CreateSemaphore(
#012 NULL, // 缺省的安全属性。
#013 0, // 初始化为0个信号量。
#014 cMax, // 最大为10个信号量。
#015 NULL); // 不命名。
#016
#017 if (m_hSemaphore == NULL)
#018 {
#019 return -1;
#020 }
#021
#022 //
#023 const int nMaxObjs = 2;
#024 HANDLE hWaitObjects[nMaxObjs] = {m_hEventExit,m_hSemaphore};
#025
#026 //线程循环。
#027 for (;;)
#028 {
#029 DWORD dwRet = WaitForMultipleObjects(nMaxObjs,hWaitObjects,FALSE,INFINITE);
#030 if (dwRet == WAIT_TIMEOUT)
#031 {
#032 //可以继续运行。
#033 TCHAR chTemp[128];
#034 wsprintf(chTemp,_T("CThreadSemaphore::Run() ThreadID=%d/r/n"),m_dwThreadID);
#035 ::OutputDebugString(chTemp);
#036
#037 //目前没有做什么事情,就让线程释放一下CPU。
#038 Sleep(10);
#039 }
#040 else if (dwRet == WAIT_OBJECT_0)
#041 {
#042 //退出线程。
#043 ::OutputDebugString(_T("Run() 退出线程/r/n"));
#044 break;
#045 }
#046 else if (dwRet == WAIT_OBJECT_0+1)
#047 {
#048 //可以继续运行。
#049 TCHAR chTemp[128];
#050 wsprintf(chTemp,_T("CThreadSemaphore::Run() Semaphore,ThreadID=%d/r/n"),m_dwThreadID);
#051 ::OutputDebugString(chTemp);
#052
#053 //
#054
#055 }
#056 else if (dwRet == WAIT_ABANDONED)
#057 {
#058 //出错。
#059 ::OutputDebugString(_T("Run() 线程出错/r/n"));
#060 return -1;
#061 }
#062 }
#063
#064 //
#065 if (m_hSemaphore)
#066 {
#067 CloseHandle(m_hSemaphore);
#068 m_hSemaphore = NULL;
#069 }
#070
#071 return 0;
#072 }
#073
第11行就是创建信号量。
第29行等信号量事件和退出事件。
#003 //增加信号量
void IncSemaphore(void)
{
if (m_hSemaphore)
{
if (!ReleaseSemaphore(
m_hSemaphore, // 要增加的信号量。
1, // 增加1.
NULL) ) // 不想返回前一次信号量。
{
}
}
}
0 0
- CreateSemaphore和ReleaseSemaphore函数
- 函数CreateSemaphore和ReleaseSemaphore
- CreateSemaphore和ReleaseSemaphore函数
- CreateSemaphore和ReleaseSemaphore函数(转)
- Windows API -- CreateSemaphore和ReleaseSemaphore函数
- Windows API一日一练(47)CreateSemaphore和ReleaseSemaphore函数
- Windows API一日一练(47)CreateSemaphore和ReleaseSemaphore函数
- Windows API一日一练(47)CreateSemaphore和ReleaseSemaphore函数
- Windows API一日一练(47)CreateSemaphore和ReleaseSemaphore函数
- Windows API一日一练(47)CreateSemaphore和ReleaseSemaphore函数
- Windows API一日一练(47)CreateSemaphore和ReleaseSemaphore函数
- Windows API一日一练(47)CreateSemaphore和ReleaseSemaphore函数
- Windows API一日一练(47)CreateSemaphore和ReleaseSemaphore函数
- Windows API一日一练(47)CreateSemaphore和ReleaseSemaphore函数
- Windows API一日一练(47)CreateSemaphore和ReleaseSemaphore函数
- Windows API一日一练(47)CreateSemaphore和ReleaseSemaphore函数
- Windows API一日一练(47)CreateSemaphore和ReleaseSemaphore函数
- Windows API一日一练(47)CreateSemaphore和ReleaseSemaphore函数
- eclipse构建maven项目出错解决办法
- Android AIDL详解
- Quartz任务调度基本使用
- flash AS3.0学习笔记----拖动棋子所停之处生成新的棋子
- Android最佳实践之UI
- 函数CreateSemaphore和ReleaseSemaphore
- 表达式求值
- 用Tomcat内置容器JNDI创建一个Session来实现注册成功后给用户发送一条邮件
- adb shell获取root权限
- 修改返回token内容与手工获取token
- 我的java开始
- 【java】【多态】的一个小例子
- LeetCode 154 -Binary Tree Postorder Traversal ( JAVA )
- 前端性能优化--文件位置问题