CMPlatform/src/Win/Semaphore.cpp

55 lines
1.3 KiB
C++
Raw Normal View History

2019-08-23 10:54:57 +08:00
#include<hgl/thread/Semaphore.h>
#include<hgl/LogInfo.h>
#include<wchar.h>
#pragma warning(disable:4800) // int -> bool 性能损失警告
namespace hgl
{
/**
* @param max_count
*/
Semaphore::Semaphore(int max_count)
{
ptr=CreateSemaphore(nullptr,0,max_count,nullptr);
if(!ptr)
LOG_ERROR(OS_TEXT("CreateSemaphore error,max_count=")+OSString(max_count));
}
Semaphore::~Semaphore()
{
CloseHandle(ptr);
}
/**
*
* @param n
* @return
*/
bool Semaphore::Post(int n)
{
if(n<=0)return(false);
return(ReleaseSemaphore(ptr,n,nullptr));
}
/**
*
* @return
*/
bool Semaphore::TryAcquire()
{
return(WaitForSingleObject(ptr,0)==WAIT_OBJECT_0);
}
/**
*
* @param time ,使0.()
* @return ,,,false
*/
bool Semaphore::Acquire(double time_out)
{
return(WaitForSingleObject(ptr,time_out>0?DWORD(time_out*1000):INFINITE)==WAIT_OBJECT_0);
}
}//namespace hgl