📄 semaphore.h
字号:
/************************************************************************
模快名: moxu 公共类库
功能: 对semaphore的简单封装
完成日期: 2007-10-20
作者: 许 培 Xu Pei(Email/MSN: peimoxu@163.com)
本代码可以自由使用,但因使用本代码造成的后果,本人不承担任何责任
************************************************************************/
#pragma once
namespace moxu
{
class Semaphore
{
private:
HANDLE m_handle;
public:
Semaphore(): m_handle(NULL){ }
~Semaphore()
{
Close();
}
bool Create(LONG initCount, LONG maxCount, LPCTSTR name = NULL)
{
m_handle = CreateSemaphore(NULL, initCount, maxCount, name);
return ((m_handle == NULL) ? false : true);
}
bool Open(DWORD desiredAccess, BOOL inheritHandle, LPCTSTR name = NULL)
{
m_handle = OpenSemaphore(desiredAccess, inheritHandle, name);
return ((m_handle == NULL) ? false : true);
}
bool Release(LONG releaseCount = 1, LONG* previousCount = NULL)
{
if(m_handle == NULL)
return false;
return ReleaseSemaphore(m_handle, releaseCount, previousCount);
}
bool Wait(DWORD milliseconds = INFINITE)
{
if(m_handle == NULL)
return false;
DWORD ret = WaitForSingleObject(m_handle, milliseconds);
return (WAIT_OBJECT_0 == ret) ? true : false;
}
bool Close()
{
if(m_handle == NULL)
return false;
int ret = CloseHandle(m_handle);
m_handle = NULL;
return ret;
}
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -