What comes first - stack unwinding or copying of return values

前端 未结 4 2216
清歌不尽
清歌不尽 2020-12-10 12:15

Is the mutex used in method GetValues() released before or after copy constructing the dummy instance?

         


        
4条回答
  •  一个人的身影
    2020-12-10 12:28

    Here is a small complete test programm (in addition to James Kanzes good explanation), which will show if the unlock is done before or after stack unwinding:

    #include 
    
    class PseudoLockGuard
    {
    public:
        enum LockState { IsStillLocked, IsUnlocked};
    
        PseudoLockGuard(LockState& value) : m_value(value) {};
        ~PseudoLockGuard() { m_value = IsUnlocked; };
    
    private:
        LockState& m_value;
    };
    
    PseudoLockGuard::LockState Test()
    {
        PseudoLockGuard::LockState indicator = PseudoLockGuard::IsStillLocked;
    
        PseudoLockGuard lock(indicator);
    
        return indicator;// Will return IsStillLocked or IsUnlocked?
    }
    
    int main(int , char** )
    {
       PseudoLockGuard::LockState result = Test();
    
       std::cout << (result == PseudoLockGuard::IsStillLocked 
                     ? "Return Value before Unlock" 
                     : "Return Value after Unlock"); 
    
       // Outputs "Return Value before Unlock"
    
       return 0;
    }
    

提交回复
热议问题