??????????????????std::lock????????
???????????????????????std::try_lock??????μ?????????????????′????????????????
??????????????????????????????е?????
?????????????????????????????????????
????std::timed_mutex
????std::timed_mutex   ??????????mutex??condition?????????????????????
????try_lock_for
????try_lock_until
???????????posix??mutex??condition??????
class timed_mutex
{
mutex              __m_;
condition_variable __cv_;
bool               __locked_;
public:
timed_mutex();
~timed_mutex();
private:
timed_mutex(const timed_mutex&); // = delete;
timed_mutex& operator=(const timed_mutex&); // = delete;
public:
void lock();
bool try_lock() _NOEXCEPT;
template
_LIBCPP_INLINE_VISIBILITY
bool try_lock_for(const chrono::duration& __d)
{return try_lock_until(chrono::steady_clock::now() + __d);}
template
bool try_lock_until(const chrono::time_point& __t);
void unlock() _NOEXCEPT;
};
template
bool
timed_mutex::try_lock_until(const chrono::time_point& __t)
{
using namespace chrono;
unique_lock __lk(__m_);
bool no_timeout = _Clock::now()
std::recursive_mutex??std::recursive_timed_mutex
?????????????????std::mutex??std::timed_mutex ??recursive??????????????????????????ε???lock()??????
??????posix mutex???recursive mutex????????
????????std::recursive_mutex?????????????
recursive_mutex::recursive_mutex()
{
pthread_mutexattr_t attr;
int ec = pthread_mutexattr_init(&attr);
if (ec)
goto fail;
ec = pthread_mutexattr_settype(&attr?? PTHREAD_MUTEX_RECURSIVE);
if (ec)
{
pthread_mutexattr_destroy(&attr);
goto fail;
}
ec = pthread_mutex_init(&__m_?? &attr);
if (ec)
{
pthread_mutexattr_destroy(&attr);
goto fail;
}
ec = pthread_mutexattr_destroy(&attr);
if (ec)
{
pthread_mutex_destroy(&__m_);
goto fail;
}
return;
fail:
__throw_system_error(ec?? "recursive_mutex constructor failed");
}
std::cv_status
??????????????condition???????????????????????????lock?????????????
????enum cv_status
????{
????no_timeout??
????timeout
????};
????std::condition_variable
?????????posix condition variable??
class condition_variable
{
pthread_cond_t __cv_;
public:
condition_variable() {__cv_ = (pthread_cond_t)PTHREAD_COND_INITIALIZER;}
~condition_variable();
private:
condition_variable(const condition_variable&); // = delete;
condition_variable& operator=(const condition_variable&); // = delete;
public:
void notify_one() _NOEXCEPT;
void notify_all() _NOEXCEPT;
void wait(unique_lock& __lk) _NOEXCEPT;
template
void wait(unique_lock& __lk?? _Predicate __pred);
template
cv_status
wait_until(unique_lock& __lk??
const chrono::time_point& __t);
template
bool
wait_until(unique_lock& __lk??
const chrono::time_point& __t??
_Predicate __pred);
template
cv_status
wait_for(unique_lock& __lk??
const chrono::duration& __d);
template
bool
wait_for(unique_lock& __lk??
const chrono::duration& __d??
_Predicate __pred);
typedef pthread_cond_t* native_handle_type;
_LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__cv_;}
private:
void __do_timed_wait(unique_lock& __lk??
chrono::time_point) _NOEXCEPT;
};
??????????????????????????????????????
????cv_status??????ж???????????????????????cv_status::timeout???????г????????cv_status::no_timeout??
????condition_variable::wait_until??????????????predicate?????????????????ж??????????????????????????????????????????
????template
????cv_status
????condition_variable::wait_until(unique_lock& __lk??
????const chrono::time_point& __t)
????{
????using namespace chrono;
????wait_for(__lk?? __t - _Clock::now());
????return _Clock::now()
????bool
????condition_variable::wait_until(unique_lock& __lk??
????const chrono::time_point& __t??
????_Predicate __pred)
????{
????while (!__pred())
????{
????if (wait_until(__lk?? __t) == cv_status::timeout)
????return __pred();
????}
????return true;
????}
????std::condition_variable_any
????std::condition_variable_any?????std::condition_variable????????????std::condition_variable??????std::unique_lock????std::condition_variable_any????????κε???????
?????????????????std::condition_variable_any??????????????????
class _LIBCPP_TYPE_VIS condition_variable_any
{
condition_variable __cv_;
shared_ptr  __mut_;
public:
condition_variable_any();
void notify_one() _NOEXCEPT;
void notify_all() _NOEXCEPT;
template
void wait(_Lock& __lock);
template
void wait(_Lock& __lock?? _Predicate __pred);
template
cv_status
wait_until(_Lock& __lock??
const chrono::time_point& __t);
template
bool
wait_until(_Lock& __lock??
const chrono::time_point& __t??
_Predicate __pred);
template
cv_status
wait_for(_Lock& __lock??
const chrono::duration& __d);
template
bool
wait_for(_Lock& __lock??
const chrono::duration& __d??
_Predicate __pred);
};
???????????????std::condition_variable_any????shared_ptr  __mut_???????mutex????????ж???????????std::unique_lock?????????mutex???????????????mutex????std::condition_variable_any??????????shared_ptr???????
??????????????????ó?std::condition_variable_any???std::condition_variable????????????
???????????????
????sched_yield()??????man???
????sched_yield() causes the calling thread to relinquish the CPU.  The thread is moved to the end of the queue for its
????static priority and a new thread gets to run.
??????C++14????std::shared_lock??std::shared_timed_mutex??????libc++????ж????????????????????
???????
????llvm libc++?е????mutex?? lock?? condition variable???????????posix???????????????????Щ????????????????
???????????????????????????????????????