一、使用场景

  在主线程中创建一个子线程去计数,计数累计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;
}

  

  

  

C++ condition_variable的更多相关文章

  1. C++11 并发指南五(std::condition_variable 详解)

    前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread,std::mut ...

  2. vs2012 condition_variable notify_one 崩溃

    vs2012项目中用到 condition_variable系统方法,程序运行过程过程中偶尔出现notify_one崩溃, 程序运行的服务器系统版本是windows server 2008 R2 SP ...

  3. 漫话C++0x(五)—- thread, mutex, condition_variable

    熟悉C++98的朋友,应该都知道,在C++98中没有thread, mutex, condition_variable这些与concurrency相关的特性支持,如果需要写多线程相关程序,都要借助于不 ...

  4. C++11并行编程-条件变量(condition_variable)详细说明

    <condition_variable >头文件主要包含有类和函数相关的条件变量. 包括相关类 std::condition_variable和 std::condition_variab ...

  5. 通过c++11的condition_variable实现的有最大缓存限制的队列

    之前曾写过一个通过C++11的condition_variable实现的有最大缓存限制的队列,底层使用std::queue来实现,如果想要提升性能的话,可以考虑改用固定的长度环形数组.环形数组实现如下 ...

  6. C++11并发——多线程条件变量std::condition_variable(四)

    https://www.jianshu.com/p/a31d4fb5594f https://blog.csdn.net/y396397735/article/details/81272752 htt ...

  7. C++并发编程 条件变量 condition_variable,线程安全队列示例

    1. 背景 c++11中提供了对线程与条件变量的更好支持,对于写多线程程序方便了很多. 再看c++并发编程,记一下学习笔记. 2. c++11 提供的相关api 3.1 wait wait用于无条件等 ...

  8. 基于std::mutex std::lock_guard std::condition_variable 和std::async实现的简单同步队列

    C++多线程编程中通常会对共享的数据进行写保护,以防止多线程在对共享数据成员进行读写时造成资源争抢导致程序出现未定义的行为.通常的做法是在修改共享数据成员的时候进行加锁--mutex.在使用锁的时候通 ...

  9. C++ 0x 使用condition_variable 与 Mutex 同步两个线程

    Mutex : 锁   同一时间只允许一个线程访问其代码内容 拟人 : 就是一把锁而已,可以lock unlock, 谁都可以拿到锁,打开门进屋,但进去后,就会把门锁上(lock) 别人想进就得等他出 ...

  10. c++11多线程学习笔记之三 condition_variable使用

    从windows角度来说,condition_variable类似event. 阻塞等待出发,不过condition_variable可以批量出发. 代码如下: // 1111111.cpp : 定义 ...

随机推荐

  1. vite2.9 + vue3.2 打包部署到nginx上刷新页面404问题

    vite2.9 + vue3.2 打包部署到nginx上刷新页面404问题 在本地运行没问题,部署到服务器上,能正常访问,但是刷新之后页面404 原有的Nginx配置为: server { liste ...

  2. python学习●错误点●expected an indented block

    报错:expected an indented block 翻译:缩进错误 因为python判断句无{},所以要缩进空格代表优先级.

  3. ABAP 报表的两种下钻功能

    在报表开发中往往会由需求要求跳转,SAP中提供了一些下钻的方式. 这里主要介绍两种 submit 和 call transaction submit 引用的是报表名称,以自开发报表居多 call tr ...

  4. JS学习-给Canvas上下文设置样式

    给Canvas上下文设置样式 <canvas class="myCanvas" width="700" height="500"> ...

  5. 三种将list转换为map的方法

    1) 传统方法假设有某个类如下 Java代码 class Movie { private Integer rank; private String description; public Movie( ...

  6. 管理名称空间资源(Kubernetes)

    管理名称空间资源 名称空间(Namespace)是Kubernetes集群级别的资源,用于将集群分隔为多个隔离的逻辑分区以配置给不同的用户.租户.环境或项目使用.例如,可以为development.q ...

  7. JavaScript 字符串的操作

    1. 在指定位置,插入字符串(此需求来源于,img中src没值的字符串标签中,插入图片http地址) // 使用slice写出的 自定义方法,绑定在String的原型链上 String.prototy ...

  8. Spring入门之使用 spring 的 IOC 解决程序耦合(Spring环境搭建)(03-01)

    3.1 案例的前期准备 1.使用的案例是:账户的业务层和持久层的依赖关系解决(就是有两个账户实现转账之类的事情,后期继续用这个案例)2.准备环境:在开始 spring 的配置之前,我们要先准备一下环境 ...

  9. 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.listenerStart Error configuring application listener of class com.titan.extend.SpringContextLoaderListener

    08-Jun-2022 10:05:21.119 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardConte ...

  10. 远程访问ubuntu电脑

    遇到的问题: 由于疫情的影响,一直在家划水,这几天突然想看看能不能通过我手中的笔记本远程访问那台在学校"落灰"的工作站呢? 目前有一台闲置的电脑,多个键已经"失灵&quo ...