admin管理员组文章数量:1599543
#include <thread>
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <future>
#include <Windows.h>
using namespace std;
class A {
public:
A() { flag = false; }
A(const A &) {}
~A() {}
bool run() {
std::unique_lock<std::mutex> _1241_lock(my_lock);
std::cerr << "waiting...\n";
cv.wait(_1241_lock, [&] {return flag == true; });
std::cerr << "...finished waiting, i == 1\n";
return true;
}
bool signal() {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
{
std::unique_lock <std::mutex> _5214_lock(my_lock);
std::cerr << "Notifying again...\n";
}
cv.notify_all();
{
std::lock_guard <std::mutex> _5242_lock();
flag = true;
std::cerr << "Notifying again...\n";
}
cv.notify_all();
return true;
}
private:
std::mutex my_lock;
std::condition_variable cv;
bool flag;
};
int main(){
A a;
//std::this_thread::sleep_for(std::chrono::milliseconds(10000));
std::future <bool> result1 = std::async(std::launch::async, &A::run, &a);
std::future <bool> result2 = std::async(std::launch::async, &A::run, &a);
std::future <bool> result3 = std::async(std::launch::async, &A::run, &a);
std::future <bool> reuslt4 = std::async(std::launch::async, &A::signal, &a);
result1.wait();
result2.wait();
result3.wait();
reuslt4.wait();
system("pause");
return 0;
}
运行结果:
本文标签: conditionvariablewait
版权声明:本文标题:[C++11]condition_variable之wait() 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1728325049a1154325.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论