1 Star 0 Fork 4

hira / CPPHelper

forked from 初雨团队 / CPPHelper 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
CriticalSectionHelper.h 1.18 KB
一键复制 编辑 原始数据 按行查看 历史
mingkuang 提交于 2017-03-02 16:06 . CPP Helper定期同步
#pragma once
#include <Windows.h>
//自动进行初始化。自动释放临界区
class CCriticalSection : private CRITICAL_SECTION
{
public:
BOOL IsInitialize;
CCriticalSection()
:IsInitialize(InitializeCriticalSectionEx(this, 0, CRITICAL_SECTION_NO_DEBUG_INFO))
{
}
//禁用复制构造
CCriticalSection(const CCriticalSection&) = delete;
//禁用赋值
CCriticalSection& operator=(const CCriticalSection&) = delete;
~CCriticalSection()
{
if(IsInitialize)
DeleteCriticalSection(this);
}
void Lock()
{
if(IsInitialize)
EnterCriticalSection(this);
}
void Unlock()
{
if (IsInitialize)
LeaveCriticalSection(this);
}
};
template<class T>
class CLockHelper
{
public:
T* pLock;
CLockHelper(T* _pLock)
:pLock(_pLock)
{
pLock->Lock();
}
~CLockHelper()
{
pLock->Unlock();
}
};
typedef CLockHelper<CCriticalSection> CCriticalLockHelper;
//读写锁
class CSRWLock : SRWLOCK
{
public:
CSRWLock()
{
InitializeSRWLock(this);
}
~CSRWLock()
{
}
void ReadLock()
{
AcquireSRWLockShared(this);
}
void ReadUnlock()
{
ReleaseSRWLockShared(this);
}
void WriteLock()
{
AcquireSRWLockExclusive(this);
}
void WriteUnLock()
{
ReleaseSRWLockExclusive(this);
}
};
C++
1
https://gitee.com/shen_hua_shan/CPPHelper.git
git@gitee.com:shen_hua_shan/CPPHelper.git
shen_hua_shan
CPPHelper
CPPHelper
master

搜索帮助