Questions tagged «condition-variable»


6
在调用condition_variable.notify_one()之前是否必须获得锁定?
我对使用有点困惑std::condition_variable。我了解我必须在致电之前创建一个unique_lockon 。我找不到的是在致电或之前是否还应该获得一个唯一的锁。mutexcondition_variable.wait()notify_one()notify_all() cppreference.com上的示例存在冲突。例如,notify_one页面给出了以下示例: #include <iostream> #include <condition_variable> #include <thread> #include <chrono> std::condition_variable cv; std::mutex cv_m; int i = 0; bool done = false; void waits() { std::unique_lock<std::mutex> lk(cv_m); std::cout << "Waiting... \n"; cv.wait(lk, []{return i == 1;}); std::cout << "...finished waiting. i == 1\n"; done = true; } void signals() …

3
调用pthread_cond_signal而不锁定互斥锁
我读到某个地方,我们应该在调用pthread_cond_signal之前锁定互斥锁,并在调用它之后解锁mutext: pthread_cond_signal()例程用于发信号(或唤醒)另一个正在等待条件变量的线程。它应该在互斥锁锁定后调用,并且必须解锁互斥锁才能完成pthread_cond_wait()例程。 我的问题是:在不锁定互斥锁的情况下调用pthread_cond_signal或pthread_cond_broadcast方法是否可以?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.