12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #include "StdAfx.h"
- #include "CriticalLock.h"
- CCriticalLock::CCriticalLock(void)
- {
- m_bInitSucc = true;
- try
- {
- //带循环等待的初始化,推荐循环4000次
- if (!InitializeCriticalSectionAndSpinCount(&m_cs, 4000))
- {
- m_bInitSucc = false;
- }
- }
- catch (...)
- {
- m_bInitSucc = false;
- }
- }
- CCriticalLock::~CCriticalLock(void)
- {
- try
- {
- DeleteCriticalSection(&m_cs);
- }
- catch (...)
- {
- }
- }
- /******************************************************************************
- 加锁
- ******************************************************************************/
- bool CCriticalLock::Lock(void)
- {
- if (!m_bInitSucc)
- {
- return false;
- }
- try
- {
- EnterCriticalSection(&m_cs);
- }
- catch (...)
- {
- return false;
- }
- return true;
- }
- /******************************************************************************
- 解锁
- ******************************************************************************/
- bool CCriticalLock::Unlock(void)
- {
- if (!m_bInitSucc)
- {
- return false;
- }
- try
- {
- LeaveCriticalSection(&m_cs);
- }
- catch (...)
- {
- return false;
- }
- return true;
- }
|