#ifndef __CRITSECTION_20160228__
#define __CRITSECTION_20160228__

/************************************************************************/
/*  Copyright (C), 2016-2020, [IT], ��������Ȩ��;
/*  ģ �� ����ThreadSection;
/*  ��    �����߳�ʹ�õ��ٽ�ֵ��������;
/*
/*  ��    ����[V];	
/*  ��    �ߣ�[IT];
/*  ��    �ڣ�[2/23/2016];
/*
/*
/*  ע    �⣺;
/*
/*  �޸ļ�¼��[IT];
/*  �޸����ڣ�;
/*  �޸İ汾��;
/*  �޸����ݣ�;
/************************************************************************/
class ThreadSection
{
public:
    ThreadSection(){
        HRESULT hr = Init();
        (hr);
    }

    ~ThreadSection(){
        DeleteCriticalSection(&_CriticalSection);
    }

    // Enter/Lock a critical section
    bool Lock(){
        bool result = false;
        __try
        {
            EnterCriticalSection(&_CriticalSection);
            result = true;
        }
        __except(STATUS_NO_MEMORY == GetExceptionCode())
        {
        }
        return result;
    }

    // Leave/Unlock a critical section
    bool Unlock(){
        bool result = false;
        __try
        {
            LeaveCriticalSection(&_CriticalSection);
            result = true;
        }
        __except(STATUS_NO_MEMORY == GetExceptionCode())
        {
        }
        return result;
    }

private:
    // Initialize critical section
    HRESULT Init() throw(){
        HRESULT hRes = E_FAIL;
        __try
        {
            InitializeCriticalSection(&_CriticalSection);
            hRes = S_OK;
        }
        // structured exception may be raised in low memory situations
        __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:
    /// auto section - constructor
    AutoThreadSection(__in ThreadSection* pSection){
		//if ( pSection != NULL ){
			_pSection = pSection;
			_pSection->Lock();
		//}
    }

    ~AutoThreadSection(){
      // if ( _pSection != NULL ) 
		   _pSection->Unlock();
    }
private:
    AutoThreadSection(const AutoThreadSection & tSection);
    AutoThreadSection &operator=(const AutoThreadSection & tSection);
    ThreadSection * _pSection;
};

#endif // __CRITSECTION_20160228__