64,644
社区成员
发帖
与我相关
我的任务
分享
CriticalSection CSocketServer::m_cs;
static std::set<const void*> g_objPtr;
static CriticalSection g_cs;
bool EvtObjMgr::IsExist(const void* objPtr)
{
Locker lock(g_cs);
return g_objPtr.find(objPtr) != g_objPtr.end();
}
class CriticalSection
{
public:
CriticalSection() { ::InitializeCriticalSection(&m_cs); }
virtual ~CriticalSection() { ::DeleteCriticalSection(&m_cs); }
void Lock() { ::EnterCriticalSection(&m_cs); }
void Unlock() { ::LeaveCriticalSection(&m_cs); }
operator PCRITICAL_SECTION() { return &m_cs; }
protected:
CRITICAL_SECTION m_cs;
};
class Locker
{
public:
Locker(PCRITICAL_SECTION cs) : m_p(cs) { ::EnterCriticalSection(m_p); }
~Locker() { ::LeaveCriticalSection(m_p); }
protected:
PCRITICAL_SECTION m_p;
};
// 维护需要处理消息的所有对象指针,只有指针存在,才能发送消息
static std::set<const void*> g_objPtr;
static CriticalSection g_cs;
void EvtObjMgr::Add(const void* objPtr)
{
Locker lock(g_cs);
g_objPtr.insert(objPtr);
}
void EvtObjMgr::Remove(const void* objPtr)
{
Locker lock(g_cs);
g_objPtr.erase(objPtr);
}