12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- #ifndef __CRITSECTION_20160221__
- #define __CRITSECTION_20160221__
- extern void WriteTextLog(const TCHAR *format, ...);
- // ÁÙ½çÖµ;
- class ThreadSection
- {
- public:
- ThreadSection(){
- HRESULT hr = Init();
- (hr);
- }
- ~ThreadSection(){
- DeleteCriticalSection(&_CriticalSection);
- }
- bool Lock()
- {
- bool result = false;
- __try
- {
- EnterCriticalSection(&_CriticalSection);
- result = true;
- }
- __except (STATUS_NO_MEMORY == GetExceptionCode())
- {
- WriteTextLog(_T("Ëøʧ°Ü,%p"), &_CriticalSection);
- }
- return result;
- }
- bool Unlock()
- {
- bool result = false;
- __try
- {
- LeaveCriticalSection(&_CriticalSection);
- result = true;
- }
- __except (STATUS_NO_MEMORY == GetExceptionCode())
- {
- WriteTextLog(_T("½âËøʧ°Ü,%p"), &_CriticalSection);
- }
- return result;
- }
- private:
- HRESULT Init() throw()
- {
- HRESULT hRes = E_FAIL;
- __try
- {
- InitializeCriticalSection(&_CriticalSection);
- hRes = S_OK;
- }
- __except (STATUS_NO_MEMORY == GetExceptionCode())
- {
- hRes = E_OUTOFMEMORY;
- }
- return hRes;
- }
- ThreadSection(const ThreadSection & tSection);
- ThreadSection &operator=(const ThreadSection & tSection);
- CRITICAL_SECTION _CriticalSection;
- };
- class AutoThreadSection
- {
- public:
- AutoThreadSection(IN ThreadSection* pSection){
- _pSection = pSection;
- _pSection->Lock();
- }
- ~AutoThreadSection(){
- _pSection->Unlock();
- }
- private:
- AutoThreadSection(const AutoThreadSection & tSection);
- AutoThreadSection &operator=(const AutoThreadSection & tSection);
- ThreadSection * _pSection;
- };
- #endif //__CRITSECTION_20160221__
|