网站建设论文html格式电子商务网站建设的背景
网站建设论文html格式,电子商务网站建设的背景,WordPress免费自动采集,网站开发工程师分析条件变量允许多个线程之间的交流。它可以阻塞某个线程#xff0c;直到另一个线程的提醒再继续#xff0c;这是通过关联一个互斥体来实现的。
本文章的代码库#xff1a;
https://gitee.com/gamestorm577/CppStd
condition_variable
condition_variable是和mutex一起使用…条件变量允许多个线程之间的交流。它可以阻塞某个线程直到另一个线程的提醒再继续这是通过关联一个互斥体来实现的。
本文章的代码库
https://gitee.com/gamestorm577/CppStd
condition_variable
condition_variable是和mutex一起使用的条件变量其提供了用于等待的接口wait、wait_for和wait_until用于通知的接口notify_one和notify_all。代码示例
std::mutex mut;
std::condition_variable cond;
bool ready false;auto Func1 []()
{std::unique_lock lock(mut);std::cout Func1 lock std::endl;cond.wait(lock);std::cout Func1 finish wait std::endl;
};auto Func2 []()
{{std::unique_lock lock(mut);std::cout Func2 lock std::endl;std::this_thread::sleep_for(std::chrono::seconds(1));}cond.notify_one();
};std::thread t1(Func1);
std::this_thread::sleep_for(std::chrono::seconds(1));
std::thread t2(Func2);
t1.join();
t2.join();在这段代码中t1线程先执行并占有了互斥mut然后t1线程调用waitt1线程进入等待的阻塞状态并解锁mut。然后t2线程开始执行占有互斥mut并在解锁mut后调用接口notify_one唤醒一个等待t1线程被唤醒继续执行接下来的代码。最后的输出结果为
Func1 lock
Func2 lock
Func1 finish wait
condition_variable_any
condition_variable_any是condition_variable的泛化可以配合更多其他类型的锁完成条件变量的功能。
notify_all_at_thread_exit
在该线程完全结束时再调用notify_all。适用于需要将局部对象析构完成再调用notify_all的场景。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/89586.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!