当CComCriticalSection::Lock可能returnE_FAIL

When CComCriticalSection::Lock may return E_FAIL

MSDN (https://msdn.microsoft.com/en-us/library/04tsf4b5.aspx) 说:

Return Value

Returns S_OK on success, E_OUTOFMEMORY or E_FAIL on failure.

获取锁可能会出现什么故障?

看起来永远不会 return。 E_FAILE_OUTOFMEMORY 的引用可能是方法 return 和 HRESULT 的标准注释。此方法可能 return 是 HRESULT 与其他方法的一致性 and/or 与其他 ATL 类.

的兼容性

这是 vc140 (2017) 工具集中 CComCriticalSection 的代码。回到 vc90 (2008) 之前的工具集具有类似的简单 Lock() 方法。 2008 年和 2017 年之间的唯一变化是添加了 SAL 属性 _Success__Acquires_lock_,它们没有任何功能影响(它们扩展为无)。我无法评论 2008 年之前的工具集。

class CComCriticalSection
{
public:
    CComCriticalSection() throw()
    {
        memset(&m_sec, 0, sizeof(CRITICAL_SECTION));
    }

    ~CComCriticalSection()
    {
    }

    _Success_(1) _Acquires_lock_(this->m_sec) HRESULT Lock() throw()
    {
        EnterCriticalSection(&m_sec);
        return S_OK;
    }
    _Success_(1) _Releases_lock_(this->m_sec) HRESULT Unlock() throw()
    {
        LeaveCriticalSection(&m_sec);
        return S_OK;
    }
    HRESULT Init() throw()
    {
        HRESULT hRes = S_OK;
        if (!_AtlInitializeCriticalSectionEx(&m_sec, 0, 0))
        {
            hRes = HRESULT_FROM_WIN32(GetLastError());
        }

        return hRes;
    }

    HRESULT Term() throw()
    {
        DeleteCriticalSection(&m_sec);
        return S_OK;
    }
    CRITICAL_SECTION m_sec;
};