????C++11?е????mutex?? lock?????????????posix??mutex??condition????????????????к????????????
????std::mutex
????????????std::mutex??
?????????????pthread_mutex_t __m_??????????????????????
C++
class mutex
{
pthread_mutex_t __m_;
public:
mutex() _NOEXCEPT {__m_ = (pthread_mutex_t)<strong>PTHREAD_MUTEX_INITIALIZER</strong>;}
~mutex();
private:
mutex(const mutex&);// = delete;
mutex& operator=(const mutex&);// = delete;
public:
void lock();
bool try_lock() _NOEXCEPT;
void unlock() _NOEXCEPT;
typedef pthread_mutex_t* native_handle_type;
_LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;}
};
mutex::~mutex()
{
pthread_mutex_destroy(&__m_);
}
void mutex::lock()
{
int ec = pthread_mutex_lock(&__m_);
if (ec)
__throw_system_error(ec?? "mutex lock failed");
}
bool mutex::try_lock() _NOEXCEPT
{
return pthread_mutex_trylock(&__m_) == 0;
}
void mutex::unlock() _NOEXCEPT
{
int ec = pthread_mutex_unlock(&__m_);
(void)ec;
assert(ec == 0);
}
??????????????std::defer_lock?? std::try_to_lock?? std::adopt_lock
???????????????????????????Щ????????????????
????std::defer_lock??????л??????
????std::try_to_lock???????????????????????
????std::adopt_lock??????????????????
???????????????????????????????????????????struct??
????struct  defer_lock_t {};
????struct  try_to_lock_t {};
????struct  adopt_lock_t {};
????constexpr defer_lock_t  defer_lock  = defer_lock_t();
????constexpr try_to_lock_t try_to_lock = try_to_lock_t();
????constexpr adopt_lock_t  adopt_lock  = adopt_lock_t();
????????????????????????????????????????????
????std::lock_guard
??????????????????????????????lock????????????????????
?????????????????
???????????????? mutext.lock()??
?????????????????????mutex.unlock() ??????
???????C++?????????????????????????????????????????????????????std::lock_guard?????????????????????????????????????mutex??????????????std::lock_guard????????
template
class lock_guard
{
public:
typedef _Mutex mutex_type;
private:
mutex_type& __m_;
public:
explicit lock_guard(mutex_type& __m)
: __m_(__m) {__m_.lock();}
lock_guard(mutex_type& __m?? adopt_lock_t)
: __m_(__m) {}
~lock_guard() {__m_.unlock();}
private:
lock_guard(lock_guard const&);// = delete;
lock_guard& operator=(lock_guard const&);// = delete;
};
???????std::lock_guard?????????????????????mutex?????????????????mutext.lock()?????????
????????????adopt_lock_t??????????????????????????????????????????
????std::unique_lock
????unique_lock?????????????????????unique???????std::lock????????????
??????????????owns_lock??????release()??????????????std::lock???????????
????owns_lock?????????ж???????????
????release()???????????????????????????????????unlock????
?????????unique_lock???????????????????????????????????????????
template
class unique_lock
{
public:
typedef _Mutex mutex_type;
private:
mutex_type* __m_;
bool __owns_;
public:
unique_lock() _NOEXCEPT : __m_(nullptr)?? __owns_(false) {}
explicit unique_lock(mutex_type& __m)
: __m_(&__m)?? __owns_(true) {__m_->lock();}
unique_lock(mutex_type& __m?? defer_lock_t) _NOEXCEPT
: __m_(&__m)?? __owns_(false) {}
unique_lock(mutex_type& __m?? try_to_lock_t)    //????
: __m_(&__m)?? __owns_(__m.try_lock()) {}
unique_lock(mutex_type& __m?? adopt_lock_t)     //????
: __m_(&__m)?? __owns_(true) {}
template
unique_lock(mutex_type& __m?? const chrono::time_point& __t)
: __m_(&__m)?? __owns_(__m.try_lock_until(__t)) {}
template
unique_lock(mutex_type& __m?? const chrono::duration& __d)
: __m_(&__m)?? __owns_(__m.try_lock_for(__d)) {}
~unique_lock()
{
if (__owns_)
__m_->unlock();
}
private:
unique_lock(unique_lock const&); // = delete;
unique_lock& operator=(unique_lock const&); // = delete;
public:
unique_lock(unique_lock&& __u) _NOEXCEPT
: __m_(__u.__m_)?? __owns_(__u.__owns_)
{__u.__m_ = nullptr; __u.__owns_ = false;}
unique_lock& operator=(unique_lock&& __u) _NOEXCEPT
{
if (__owns_)
__m_->unlock();
__m_ = __u.__m_;
__owns_ = __u.__owns_;
__u.__m_ = nullptr;
__u.__owns_ = false;
return *this;
}
void lock();
bool try_lock();
template
bool try_lock_for(const chrono::duration& __d);
template
bool try_lock_until(const chrono::time_point& __t);
void unlock();
void swap(unique_lock& __u) _NOEXCEPT
{
_VSTD::swap(__m_?? __u.__m_);
_VSTD::swap(__owns_?? __u.__owns_);
}
mutex_type* release() _NOEXCEPT
{
mutex_type* __m = __m_;
__m_ = nullptr;
__owns_ = false;
return __m;
}
bool owns_lock() const _NOEXCEPT {return __owns_;}
operator bool () const _NOEXCEPT {return __owns_;}
mutex_type* mutex() const _NOEXCEPT {return __m_;}
};
????std::lock??std::try_lock????
??????????????????????????????
????std::lock??std::try_lock???????????????????????????????????????????????????д?????????????????????????????????
???????????std::try_lock????????????
????????????????-1??
????????????????????????л?????????0?????????
?????????????????????????????????????????????????????????д????£?
template
void
lock(_L0& __l0?? _L1& __l1)
{
while (true)
{
{
unique_lock __u0(__l0);
if (__l1.try_lock())  //??????l0?????????l1
{
__u0.release();   //l0??l1?????????????unique_lock???????????l0???????????release()???????????????l0????
break;
}
}//????????l0??l1????????????l0??
sched_yield();  //?????????????????????е?β????CPU?л?????????????
{
unique_lock __u1(__l1); //??????波??????l1????????б??????????l1?????????????????l1?????????????????????????????
if (__l0.try_lock())
{
__u1.release();
break;
}
}
sched_yield();
}
}
template
int
try_lock(_L0& __l0?? _L1& __l1)
{
unique_lock __u0(__l0?? try_to_lock);
if (__u0.owns_lock())
{
if (__l1.try_lock()) //???try_lock?????????壬??????????????
{
__u0.release();
return -1;
}
else
return 1;
}
return 0;
}
?????????lock?????ó????????????????
????????????????????????????????????????????
????????????std::try_lock??????????
????????????????try_lock?????????????????????????????????????е?unique_lock??release???
?????????????????????????????????
template
int
try_lock(_L0& __l0?? _L1& __l1?? _L2& __l2?? _L3&... __l3)
{
int __r = 0;
unique_lock __u0(__l0?? try_to_lock);
if (__u0.owns_lock())
{
__r = try_lock(__l1?? __l2?? __l3...);
if (__r == -1)
__u0.release();
else
++__r;
}
return __r;
}
?????????????????std::lock??????
template
void
__lock_first(int __i?? _L0& __l0?? _L1& __l1?? _L2& __l2?? _L3& ...__l3)
{
while (true)
{
switch (__i)  //__i???????????λ?????????????????????0???????
{
case 0:   //???????????__i??0
{
unique_lock __u0(__l0);
__i = try_lock(__l1?? __l2?? __l3...);
if (__i == -1)  //?????l0?????????????????????????????????????????????????unique_lock?release????????
{
__u0.release();
return;
}
}
++__i;  //???__i?????????????????????????try_lock(__l1??__l2__l3??...)???l1??????????????+1??????????л?????????????′??????????????
sched_yield();
break;
case 1:   //?????λ??l1?????????????l1??
{
unique_lock __u1(__l1);
__i = try_lock(__l2?? __l3...?? __l0);   //?????ε?l0????????????????l1??????????????????
if (__i == -1)
{
__u1.release();
return;
}
}
if (__i == sizeof...(_L3) + 1)   //?????l0??????????????l0?????????????????????????????l0?????????????l0????????
__i = 0;
else
__i += 2; //???__i?????????????????????????try_lock(__l2??__l3...?? __l0)???l2??????????????+2
sched_yield();
break;
default:
__lock_first(__i - 2?? __l2?? __l3...?? __l0?? __l1);    //??????????l2?????????__i???2??
return;
}
}
}
template
inline _LIBCPP_INLINE_VISIBILITY
void
lock(_L0& __l0?? _L1& __l1?? _L2& __l2?? _L3& ...__l3)
{
__lock_first(0?? __l0?? __l1?? __l2?? __l3...);
}