【发布时间】:2022-10-08 04:17:48
【问题描述】:
我从https://en.cppreference.com/w/cpp/thread/condition_variable/wait 读到wait() “原子地解锁锁”。我如何通过std::cout 看到这个?我试图更好地理解条件变量的实际作用。我在下面写了一个尝试。
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>
using namespace std;
condition_variable cv;
mutex m;
bool stopped = false;
void f1() {
unique_lock<mutex> ul{m};
cout << "f1: " << ul.owns_lock() << endl;
cv.wait(ul, [&]{
cout << "f1: " << ul.owns_lock() << endl;
return stopped;
});
cout << "f1 RUNNING\n";
cout << "f1: " << ul.owns_lock() << endl;
}
void f2() {
lock_guard<mutex> lg{m};
cout << "f2 RUNNING\n";
}
int main() {
unique_lock<mutex> ul{m};
thread t1(&f1);
thread t2(&f2);
cout << ul.owns_lock() << endl;
this_thread::sleep_for(chrono::seconds(1));
stopped = true;
cv.notify_one();
cout << ul.owns_lock() << endl;
ul.unlock();
cout << ul.owns_lock() << endl;
this_thread::sleep_for(chrono::seconds(1));
t1.join();
t2.join();
return 0;
}
【问题讨论】:
-
您永远不会看到 owns_lock() 返回 false,因为在 wait() 解锁互斥锁后线程立即进入睡眠状态。您可以 notify() 线程,然后它将执行谓词函数以确定是否继续等待,但在此检查期间将重新获取互斥锁,并且 owns_lock() 将返回 true。
标签: c++ c++11 std conditional-variable