提问人:davidj361 提问时间:10/8/2022 更新时间:10/9/2022 访问量:295
显示从 std::condition_variable::wait 解锁
Showing the unlock from std::condition_variable::wait
问:
我从 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;
}
答:
3赞
Remy Lebeau
10/8/2022
#1
std::unique_lock
并使用满足 BasicLockable 要求的任何类类型。因此,只需编写自己的类来包装 ,然后就可以添加所需的任何日志记录。std::lock_guard
std::mutex
更新:但是,仅适用于,因此如果您编写自己的互斥包装器类,则必须改用。std:condition_variable
std::mutex
std::condition_variable_any
例如:
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>
using namespace std;
struct LoggingMutex
{
mutex m;
void lock() {
cout << "Locking" << endl;
m.lock();
cout << "Locked" << endl;
}
bool try_lock() {
cout << "Attempting to lock" << endl;
bool result = m.try_lock();
cout << (result ? "Locked" : "Not locked") << endl;
return result;
}
void unlock() {
cout << "Unlocking" << endl;
m.unlock()
cout << "Unlocked" << endl;
}
};
condition_variable_any cv;
LoggingMutex lm;
bool stopped = false;
void f1() {
unique_lock<LoggingMutex> ul{lm};
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<LoggingMutex> lg{lm};
cout << "f2 RUNNING\n";
}
int main() {
unique_lock<LoggingMutex> ul{lm};
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;
}
评论
1赞
Igor Tandetnik
10/9/2022
std::condition_variable
具体采取;它不适用于 .您的示例实际上无法编译。您可能打算使用std::unique_lock<std::mutex>
unique_lock<LoggingMutex>
std::condition_variable_any
评论