发件人:https://en.cppreference.com/w/cpp/thread/condition_variable/wait
void waits()
{
std::unique_lock<std::mutex> lk(cv_m);
std::cerr << "Waiting... \n";
cv.wait(lk, []{return i == 1;});
std::cerr << "...finished waiting. i == 1\n";
}
第二个参数是谓词:
template< class Predicate >
void wait( std::unique_lock<std::mutex>& lock, Predicate pred );
[]
在那做什么?在i == 1
中写入条件wait
与此有何不同:
if (i == 1)
{
std::unique_lock<std::mutex> lk( mutex_association );
// Do something with `i`
}
else
{
std::unique_lock<std::mutex> lk( mutex_association );
cv_association.wait( lk );
}