一、使用场景

  在主线程中创建一个子线程去计数,计数累计100次后认为成功,并告诉主线程;主线程收到计数100次完成的信息后继续往下执行

二、条件变量的成员函数

wait:当前线程调用 wait() 后将被阻塞,直到另一个线程调用 noteify() 唤醒当前线程,使其被阻塞的线程继续运行。

①void wait(unique_lock<mutex>& _Lck)();

②template <class _Predicate>

void wait(unique_lock<mutex>& _Lck, _Predicate _Pred) ;

_Pred : wait 的预测条件,

只有当 _Pred 为 false 且获取锁后调用 wait() 才会阻塞当前线程;

只有当 _Pred 为 true 且获取锁后 收到唤醒通知后 才会解除阻塞;

wait_for:可以指定一个时间段,在当前线程收到唤醒通知或指定的时间超时之前,该线程都会处于阻塞状态;超时或收到线程通知后返回

enum class cv_status { 
no_timeout,
timeout };

①cv_status wait_for(unique_lock<mutex>& _Lck, const chrono::duration<_Rep, _Period>& _Rel_time) ;

②template <class _Rep, class _Period, class _Predicate>
bool wait_for(unique_lock<mutex>& _Lck, const chrono::duration<_Rep, _Period>& _Rel_time, _Predicate _Pred);

_Rel_time:等待的时间段,_Pred : wait_for的预测条件

当 _Pred 为 true   时,立刻唤醒线程,返回 true(_Pred 的状态),无需等待超时时间; 

当 _Pred 为 false 时,超过指定时间段未收到 notify_one 信号,唤醒线程,返回 false

定时间段内收到 notify_one 信号时,取决于_Pred 的状态,若为 _Pred 为 false,线程依然阻塞,返回 false(_Pred 的状态)

三、使用方法

#include <iostream>#include <thread>#include <mutex>#include <condition_variable>
int main(){   /************************可以作为 While 的条件**************************/   int i = 0;   bool while_Out = false;   std::mutex while_mtx;   std::condition_variable while_cv;   std::unique_lock<std::mutex>guard(while_mtx);   while (while_cv.wait_for(guard, std::chrono::milliseconds(10), [&] {return while_Out == true; }) == false)   {//当 while_Out 为 false 时,等待 10ms,返回 false,进入 while 循环     if (i == 50)     {         while_Out = true;//当 while_Out 为 true 时,下次进行 wait_for 时无需等待超时时间,立刻返回 true ,结束循环         while_cv.notify_one();//因为是同步,所以 notify_one 没有作用,要先走完 while     }     i++;     std::cout << i << std::endl;//所以会输出51   }   std::cout << "test finish" << std::endl;   /************************可以作为 While 的条件**************************/   return 0;}
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
#include <condition_variable>
#include <ctime>
#include <thread>
#include <iomanip> void Get_time()
{
std::time_t newTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
auto formatTime = std::put_time(std::localtime(&newTime), "%Y-%m-%d %X");
std::cout << "current time= " << formatTime << std::endl;
} bool g_Out = false;
std::mutex g_mtx;
std::condition_variable g_cv; void test()
{
int i = 0;
while (true)
{
i++;
if (i == 50)
{
Get_time();
g_Out = true;
g_cv.notify_one();
std::cout << " 唤醒主线程" << std::endl;
}
}
} int main()
{
g_Out = false;//阻塞主线程
std::thread t1(test);
t1.detach();
std::unique_lock<std::mutex>guard(g_mtx);
Get_time();
std::cout << " 阻塞主线程" << std::endl;
int ret = g_cv.wait_for(guard, std::chrono::minutes(1), [&] {return g_Out == true; });
std::cout << ret << std::endl;
//当 _Pred == false 时,在指定时间段(1分钟)内进入阻塞状态,
//如果一直未接收到 notify_one 信号则超时唤醒线程,返回 _Pred 值
//如果中途接收到 notify_one 信号:
//①_Pred == true 则唤醒线程,返回 true;
//②_Pred == false 则依然阻塞线程,直至超时返回 _Pred 值;
return 0;
}

  

  

  

最新文章

  1. querySelectorAll 方法相比 getElementsBy 系列方法区别
  2. Hibernate核心技术简介
  3. 硬件抽象层:HAL
  4. 【BZOJ 1492】【NOI 2007】货币兑换Cash
  5. C#文本选中及ContextMenuStrip菜单使用
  6. MyBatis知多少(26)调试
  7. 采用handle消息机制实现轮播效果
  8. shiro安全框架
  9. 修改netbeans模版头部的说明
  10. ZOJ 2625 Rearrange Them(DP)
  11. linux 小技巧总结
  12. nfs error
  13. ajaxfileupload.js插件结合一般处理文件实现Ajax无刷新上传
  14. Zabbix 监控rabbitmq
  15. 洛谷 P4016负载平衡问题【费用流】题解+AC代码
  16. GDAL库扩展Landsat系列MTL文件格式支持
  17. js 转java后台传过来的list
  18. SQL 数据插入、删除 大数据
  19. 阿里云 Windows Server 2012 r2 部署asp.net mvc网站 平坑之旅
  20. ERROR: child process failed, exited with error number 100

热门文章

  1. pytorch 独热编码报错的解决办法:one_hot is only applicable to index tensor
  2. mybatis核心配置文件—设置别名typeAliases
  3. JS实现另存/打印功能
  4. TypeScript 合并以及删除数组数据
  5. freeswitch开启https,wss
  6. vue后台管理系统——权限管理模块
  7. SpringBoot写第一个接口
  8. 2020年第11届蓝桥杯C/C++B组 第二轮省赛
  9. AXI4_LITE总线vivado2019.1官方模板源码(verilog实现)
  10. curl 查看响应时间