zoukankan      html  css  js  c++  java
  • ATL 临界区

    • CComAutoCriticalSection Contains methods for obtaining and releasing a critical section. The critical section is automatically initialized.

    • CComCriticalSection Contains methods for obtaining and releasing a critical section. The critical section must be explicitly initialized.

    • CComFakeCriticalSection Mirrors the methods in CComCriticalSection without providing a critical section. The methods in CComFakeCriticalSection do nothing.

    • CComSafeDeleteCriticalSection When an instance of CComSafeDeleteCriticalSection goes out of scope or is explicitly deleted from memory, the underlying critical section object will automatically be cleaned up if it is still valid. In addition, the CComSafeDeleteCriticalSection::Term method will exit gracefully if the underlying critical section object has not yet been allocated or has already been released from memory.
    class CComCriticalSection
    {
    public:
        CComCriticalSection() throw()
        {
            memset(&m_sec, 0, sizeof(CRITICAL_SECTION));
        }
        ~CComCriticalSection()
        {
        }
        HRESULT Lock() throw()
        {
            EnterCriticalSection(&m_sec);
            return S_OK;
        }
        HRESULT Unlock() throw()
        {
            LeaveCriticalSection(&m_sec);
            return S_OK;
        }
        HRESULT Init() throw()
        {
            HRESULT hRes = S_OK;
    
            if (!InitializeCriticalSectionAndSpinCount(&m_sec, 0))
            {
                hRes = HRESULT_FROM_WIN32(GetLastError());
            }
    
            return hRes;
        }
    
        HRESULT Term() throw()
        {
            DeleteCriticalSection(&m_sec);
            return S_OK;
        }
        CRITICAL_SECTION m_sec;
    };
    
    class CComAutoCriticalSection : 
        public CComCriticalSection
    {
    public:
        CComAutoCriticalSection()
        {
            HRESULT hr = CComCriticalSection::Init();
            if (FAILED(hr))
                AtlThrow(hr);
        }
        ~CComAutoCriticalSection() throw()
        {
            CComCriticalSection::Term();
        }
    private :
        HRESULT Init(); // Not implemented. CComAutoCriticalSection::Init should never be called
        HRESULT Term(); // Not implemented. CComAutoCriticalSection::Term should never be called
    };
    
    class CComSafeDeleteCriticalSection : 
        public CComCriticalSection
    {
    public:
        CComSafeDeleteCriticalSection(): m_bInitialized(false)
        {
        }
    
        ~CComSafeDeleteCriticalSection() throw()
        {
            if (!m_bInitialized)
            {
                return;
            }
            m_bInitialized = false;
            CComCriticalSection::Term();
        }
    
        HRESULT Init() throw()
        {
            ATLASSERT( !m_bInitialized );
            HRESULT hr = CComCriticalSection::Init();
            if (SUCCEEDED(hr))
            {
                m_bInitialized = true;
            }
            return hr;
        }
    
        HRESULT Term() throw()
        {
            if (!m_bInitialized)
            {
                return S_OK;
            }
            m_bInitialized = false;
            return CComCriticalSection::Term();
        }
    
        HRESULT Lock()
        {
            // CComSafeDeleteCriticalSection::Init or CComAutoDeleteCriticalSection::Init
            // not called or failed.
            // m_critsec member of CComObjectRootEx is now of type
            // CComAutoDeleteCriticalSection. It has to be initialized
            // by calling CComObjectRootEx::_AtlInitialConstruct
            ATLASSUME(m_bInitialized);
            return CComCriticalSection::Lock();
        }
    
    private:
        bool m_bInitialized;
    };
    
    class CComAutoDeleteCriticalSection : 
        public CComSafeDeleteCriticalSection
    {
    private:
        // CComAutoDeleteCriticalSection::Term should never be called
        HRESULT Term() throw();
    };
    
    class CComFakeCriticalSection
    {
    public:
        HRESULT Lock() throw()
        {
            return S_OK;
        }
        HRESULT Unlock() throw()
        {
            return S_OK;
        }
        HRESULT Init() throw()
        {
            return S_OK;
        }
        HRESULT Term() throw()
        {
            return S_OK;
        }
    };
    

    CComCritSecLock

    This class provides methods for locking and unlocking a critical section object.

    配合CComCriticalSection使用

    template< class TLock >
    class CComCritSecLock
    {
    public:
        CComCritSecLock(
            _Inout_ TLock& cs,
            _In_ bool bInitialLock = true );
        ~CComCritSecLock() throw();
    
        HRESULT Lock() throw();
        void Unlock() throw();
    
    // Implementation
    private:
        TLock& m_cs;
        bool m_bLocked;
    
    // Private to avoid accidental use
        CComCritSecLock(_In_ const CComCritSecLock&) throw();
        CComCritSecLock& operator=(_In_ const CComCritSecLock&) throw();
    };
    
    template< class TLock >
    inline CComCritSecLock< TLock >::CComCritSecLock(
            _Inout_ TLock& cs,
            _In_ bool bInitialLock) :
        m_cs( cs ),
        m_bLocked( false )
    {
        if( bInitialLock )
        {
            HRESULT hr;
    
            hr = Lock();
            if( FAILED( hr ) )
            {
                AtlThrow( hr );
            }
        }
    }
    
    template< class TLock >
    inline CComCritSecLock< TLock >::~CComCritSecLock() throw()
    {
        if( m_bLocked )
        {
            Unlock();
        }
    }
    
    template< class TLock >
    inline HRESULT CComCritSecLock< TLock >::Lock() throw()
    {
        HRESULT hr;
    
        ATLASSERT( !m_bLocked );
        hr = m_cs.Lock();
        if( FAILED( hr ) )
        {
            return( hr );
        }
        m_bLocked = true;
    
        return( S_OK );
    }
    
    template< class TLock >
    inline void CComCritSecLock< TLock >::Unlock() throw()
    {
        ATLASSUME( m_bLocked );
        m_cs.Unlock();
        m_bLocked = false;
    }
    
  • 相关阅读:
    修改Android系统属性SystemProperties.set("sys.powerctl", "shutdown")关机分析
    android手动修改density(dpi)的方法
    nodejs+express+mongodb 快速接口开发
    《杜增强讲Unity之Tanks坦克大战》10-相机控制
    《杜增强讲Unity之Tanks坦克大战》11-游戏流程控制
    《杜增强讲Unity之Tanks坦克大战》9-发射子弹时蓄力
    《杜增强讲Unity之Tanks坦克大战》8-子弹碰撞处理
    《杜增强讲Unity之Tanks坦克大战》7-坦克血条
    《杜增强讲Unity之Tanks坦克大战》6-发射子弹
    《杜增强讲Unity之Tanks坦克大战》5-子弹
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/2674800.html
Copyright © 2011-2022 走看看