GCC C ++ 11 Condition Variables Waiting for Internal

I was looking for an error that we encounter with some mutable classes of modified flows / conditions that are updated to use C ++ 11 streams. During the hunt, I met the following in the GCC code base:

  template<typename _Lock>
  void
  wait(_Lock& __lock)
  {
    unique_lock<mutex> __my_lock(_M_mutex);
    _Unlock<_Lock> __unlock(__lock);
    // _M_mutex must be unlocked before re-locking __lock so move
    // ownership of _M_mutex lock to an object with shorter lifetime.
    unique_lock<mutex> __my_lock2(std::move(__my_lock));
    _M_cond.wait(__my_lock2);
  }

Despite the comment, it's hard for me to understand the purpose of the move constructor here in __my_lock2. Why has __my_lock moved to __my_lock2 here?

+4
source share
3 answers

This is not like code condition_variable. It looks like a code condition_variable_any. The latter has a wait pattern function. The first does not.

N2406 , . N2406 condition_variable_any gen_cond_var, . , , , , _M_mutex __lock .

, , N2406, , , , , N2406. _Unlock, . - N2406 :

}  // mut_.unlock(), external.lock()

.. __lock. __my_lock , __unlock, ,

}  // _M_mutex.unlock(), __lock.lock()

. , , N2406 ( ).

+10

(, , std::condition_variable_any<_Lock>::wait), , , __lock _M_mutex. - , __lock _M_cond. , _Unlock<_Lock> RAII . , . :

  • __lock ( wait)
  • _M_mutex ( __my_lock)
  • release __lock ( __unlock)
  • _M_mutex _M_cond ( _M_cond.wait)
  • reacquire _M_mutex ( _M_cond.wait)
  • release _M_mutex ( __my_lock2)
  • reacquire __lock ( __unlock)

__my_lock __my_lock2 , __my_lock2 __unlock, , 6 7, .

+3

, __lock __my_lock.

:

construct __my_lock // locks   _M_mutex
construct __unlock  // unlocks __lock
construct __my_lock2 // Does nothing as its a move.

_M_cond.wait(__my_lock2);

destroy __mylock2 // unlocks __M_mutex
destroy __unlock // locks __lock again
destroy __mylock // does nothing as its been moved

construct __my_lock // locks   _M_mutex
construct __unlock  // unlocks __lock

_M_cond.wait(__my_lock);

destroy __unlock // locks __lock
destroy __mylock // unlocks _M_mutex

,

+1

Source: https://habr.com/ru/post/1529923/


All Articles