服务端框架之同步事件

来源:互联网 发布:g71车盲孔编程实例 编辑:程序博客网 时间:2024/05/15 05:55
#include "CEvent.h"#include <cassert>#include "CMutex.h"#include "CSystemLog.h"#ifdef _WIN32#pragma warning(disable:4800)#endifCCEvent::CCEvent(void){DT_BOOL ret = Create();assert(ret == DT_TRUE);}CCEvent::~CCEvent(void){Close();}DT_BOOL CCEvent::Create(){#ifdef _WIN32m_handle = CreateEvent(0, DT_TRUE, DT_FALSE, 0);return m_handle != 0;#elsesigemptyset(&m_sigs);sigaddset(&m_sigs, SIGUSR1);sigprocmask(SIG_BLOCK, &m_sigs, 0);    return DT_TRUE;//return pthread_cond_init(&m_handle, 0) == 0;#endif}//上锁DT_BOOL CCEvent::Lock(){#ifdef _WIN32DT_BOOL ret = ResetEvent(m_handle);//设置为无信号量WaitForSingleObject(m_handle , INFINITE);//如果dwMilliseconds为INFINITE,对象被触发信号后,函数才会返回。线程会阻塞在此return ret;#else//堵塞信号DT_INT32    sig; sigwait(&m_sigs, &sig);return DT_TRUE;// return pthread_cond_wait(&m_handle, &(mutex.m_handle)) == 0;#endif }//解锁DT_BOOL CCEvent::UnLock(DT_DWORD pThread){#ifdef _WIN32return SetEvent(m_handle);//设置为有信号量#elsepthread_t* pTid = (pthread_t*)pThread;pthread_kill(*pTid, SIGUSR1);return DT_TRUE;       // return  pthread_cond_signal(&m_handle) == 0;#endif}//关闭void CCEvent::Close(){#ifdef _WIN32CloseHandle(m_handle);#else      //    pthread_cond_destroy(&m_handle);#endif}

0 0
原创粉丝点击