#else
#include <pthread.h>
#include <semaphore.h>
typedef int TSemWaitTimeout;
class TSemaphore
{
private:
sem_t m_sem;
public:
TSemaphore()
{
m_sem = NULL;
};
~TSemaphore()
{
if(NULL != m_sem)
{
sem_dstroy(&m_sem);
}
};
//创建信号量
bool Create(long lInitialCount, long lMaximumCount,const char* pName = NULL)
{
if (NULL != m_sem)
{
return false;
}
sem_init(&m_sem, 0, lMaximumCount);
if (NULL != m_sem && lInitialCount > 0)
{
for (long i = 0; i < lInitialCount; i++)
{
sem_wait(&m_sem);
}
return true;
}
return false;
};
//等待信号量
bool Wait(TSemWaitTimeout dwMilliseconds= INFINITE )
{
if (NULL == m_sem)
{
return false;
}
if(dwMilliseconds == INFINITE)
{
return sem_wait(&m_sem) == 0;
}
else
{
/*
struct timespec {
time_t tv_sec; /* Seconds */
/*long tv_nsec; /* Nanoseconds [0 .. 999999999]
}*/
timespec time;
time.tv_sec = dwMilliseconds/1000;
time.tv_nsec = dwMilliseconds%1000 * 1000000;
return sem_timedwait(&m_sem,&time) == 0;
}
};
//释放信号量
bool Release()
{
return sem_post(&sem) == 0;
}
}TSemaphore;
#endif