还是早些年的代码....
/********************************************************************
* Copyright* All rights reserved
*
* 文件名称: posix_sem.h
* 文件标示:
* 摘 要: 基于 posix_sem 实现的信号量
* 线程安全: 是
*
* 当前版本: 1.0
* 作 者:
* 完成日期: 2010-9-8 10:12:09
*
* 备注
********************************************************************/
#ifndef _POSIX_SEM_H_
#define _POSIX_SEM_H_
#include "basic.h"
#include "datetime.h"
#include <semaphore.h>
#define SEM_PROCESS_PRIVATE 0
#define SEM_PROCESS_SHARED 1
//信号量
class CSem
{
public:
CSem( uint32_t uiValue = 0 )
{
sem_init(&m_sem,SEM_PROCESS_PRIVATE,uiValue);
}
~CSem()
{
sem_destroy(&m_sem);
}
//获取当前信号量的值 -1表示失败
int GetValue()
{
int iTmp = -1;
if (sem_getvalue(&m_sem,&iTmp) != 0)
{
return -1;
}
return iTmp;
}
//释放资源
bool Post()
{
return (sem_post(&m_sem) == 0);
}
//等待
bool Wait()
{
return (sem_wait(&m_sem) == 0);
}
//超时等待 单位MS
bool TimedWait(int iMilliSeconds)
{
struct timespec timewake;
memset(&timewake,0,sizeof(timewake));
CDateTimeH::AbsTimeFromNow(iMilliSeconds,&timewake);
return (sem_timedwait(&m_sem,&timewake) == 0);
}
protected:
CSem(const CSem & );
const CSem & operator= (const CSem&);
private:
sem_t m_sem;
};
//命名 信号量
class CNamedSem
{
public:
CNamedSem(const char * pszSemName)
:m_strSemName(string(pszSemName) + string(".sem")),m_pSem(NULL)
{
}
~CNamedSem()
{
}
bool Create(uint32_t uiValue = 0); //创建命名信号量
bool Attach(); //关联命名信号量
//获取当前信号量的值 -1表示失败
int GetValue()
{
int iTmp = -1;
if (m_pSem == NULL)
{
return -1;
}
if (sem_getvalue(m_pSem,&iTmp) != 0)
{
return -1;
}
return iTmp;
}
//释放资源
bool Post()
{
if (m_pSem == NULL)
{
return false;
}
return (sem_post(m_pSem) == 0);
}
//等待
bool Wait()
{
if (m_pSem == NULL)
{
return false;
}
return (sem_wait(m_pSem) == 0);
}
//超时等待 单位MS
bool TimedWait(int iMilliSeconds)
{
struct timespec timewake;
if (m_pSem == NULL)
{
return false;
}
memset(&timewake,0,sizeof(timewake));
CDateTimeH::AbsTimeFromNow(iMilliSeconds,&timewake);
return (sem_timedwait(m_pSem,&timewake) == 0);
}
//删除信号量
bool Remove();
protected:
CNamedSem(const CNamedSem & );
const CNamedSem & operator= (const CNamedSem&);
private:
sem_t * m_pSem;
string m_strSemName;
};
#endif
----------------------------------------------------------------------------------------------------- 华丽的分割线 ------------------------------------------------------------------------------------
/********************************************************************
* Copyright
* All rights reserved
*
* 文件名称: posix_sem.cpp
* 文件标示:
* 摘 要: 基于 posix_sem 实现的信号量
* 线程安全: 是
*
* 当前版本: 1.0
* 作 者:
* 完成日期: 2011-2-21 16:56:53
*
* 备注
********************************************************************/
#include "posix_sem.h"
#include "posix_ipcs.h"
/*
功能: 初始化锁
参数:
无
返回:
bool 成功
false 失败
*/
bool CNamedSem::Create(uint32_t uiValue)
{
CPosixShm Shm(m_strSemName.c_str());
m_pSem = (sem_t *)Shm.Alloc(sizeof(sem_t));
if (m_pSem == NULL)
{
return false;
}
return sem_init(m_pSem,SEM_PROCESS_PRIVATE,uiValue) == 0;
}
/*
功能: 关联锁的共享内存
参数:
无
返回:
bool 成功
false 失败
*/
bool CNamedSem::Attach()
{
CPosixShm Shm(m_strSemName.c_str());
if (!Shm.Attach())
{
return false;
}
m_pSem = (sem_t *)Shm.GetShmPoint();
if (m_pSem == NULL)
{
return false;
}
return true;
}
/*
功能: 删除共享内存中的命名信号量
参数:
无
返回:
bool 成功
false 失败
*/
bool CNamedSem::Remove()
{
CPosixShm Shm(m_strSemName.c_str());
if (!Shm.Attach())
{
return false;
}
return Shm.Remove();
}