// ConsoleApplication6.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include<thread>
#include<iostream>
#include<list>
#include<mutex>
using namespace std;
mutex mut_one;
once_flag gl_flag;//标记 class A
{
private:
list<int> msgRecvQueue;
mutex my_mutex;

my_cond;//生成一个条件对象
static A* Instance;
public:
static A* GetInstance()
{
if (Instance == NULL)
{
unique_lock<mutex> my(mut_one);
if (Instance == NULL)
{
Instance = new A;
static A_Guard gl;
}
}
return Instance;
} class A_Guard
{
public:
~A_Guard()
{
if (A::Instance != NULL)
{
delete A::Instance;
A::Instance = NULL;
}
}
};
void enmsg()
{
for (int i = 1; i <= 100; i++)
{
msgRecvQueue.push_back(i);
cout << "压入数据成功,数据为:" << i << endl;
my_cond.notify_one();//作用是为了唤醒堵塞的wait
}
} void outmsg()
{
int command = 0;
while (true)
{
unique_lock<mutex> sbg(my_mutex);
my_cond.wait(sbg, [this] {
if (!msgRecvQueue.empty())
return true;
else
return false;
});//第二个参数如果返回false,wait会解锁,并堵塞在这一行等待notify_one
//上面假设notify_one执行了唤醒的操作,那么第二个参数里面的list就不为空了,会返回true,
//返回true之后也就意味着不堵塞了,继续执行以下的代码
command = msgRecvQueue.front();
cout << "弹出来的数据是" << command << endl;
msgRecvQueue.pop_front();
}
} }; A* A::Instance = NULL;
void Thread_one()
{
A *s = A::GetInstance();
s->enmsg();
}
void Thread_two()
{
A*s = A::GetInstance();
s->outmsg();
}
int main()
{ thread thone(Thread_one);
thread thtwo(Thread_two);
thone.join();
thtwo.join(); return 0;
}

把notify_one变为notify_all的代码:

// ConsoleApplication6.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include<thread>
#include<iostream>
#include<list>
#include<mutex>
using namespace std;
mutex mut_one;
once_flag gl_flag;//标记 class A
{
private:
list<int> msgRecvQueue;
mutex my_mutex;
condition_variable my_cond;//生成一个条件对象
static A* Instance;
public:
static A* GetInstance()
{
if (Instance == NULL)
{
unique_lock<mutex> my(mut_one);
if (Instance == NULL)
{
Instance = new A;
static A_Guard gl;
}
}
return Instance;
} class A_Guard
{
public:
~A_Guard()
{
if (A::Instance != NULL)
{
delete A::Instance;
A::Instance = NULL;
}
}
};
void enmsg()
{
for (int i = 1; i <= 100; i++)
{
msgRecvQueue.push_back(i);
cout << "压入数据成功,数据为:" << i << endl;
//my_cond.notify_one();//作用是为了唤醒堵塞的wait
my_cond.notify_all();//故名思意,唤醒的不知一个wait
}
} void outmsg()
{
int command = 0;
while (true)
{
unique_lock<mutex> sbg(my_mutex);
my_cond.wait(sbg, [this] {
if (!msgRecvQueue.empty())
return true;
else
return false;
});//第二个参数如果返回false,wait会解锁,并堵塞在这一行等待notify_one
//上面假设notify_one执行了唤醒的操作,那么第二个参数里面的list就不为空了,会返回true,
//返回true之后也就意味着不堵塞了,继续执行以下的代码
command = msgRecvQueue.front();
cout << "弹出来的数据是" << command << endl;
msgRecvQueue.pop_front();
}
} }; A* A::Instance = NULL;
void Thread_one()
{
A *s = A::GetInstance();
s->enmsg();
}
void Thread_two()
{
A*s = A::GetInstance();
s->outmsg();
}
void Thread_three()
{
A*s = A::GetInstance();
s->outmsg();
}
int main()
{ thread thone(Thread_one);
thread thtwo(Thread_two);
thread three(Thread_three);
thone.join();
thtwo.join();
three.join();
return 0;
}

把其中的一部分拿出来分析一下,比如这段代码:

while (true)
{
unique_lock<mutex> sbg(my_mutex);
my_cond.wait(sbg, [this] {
if (!msgRecvQueue.empty())
return true;
else
return false;
});//第二个参数如果返回false,wait会解锁,并堵塞在这一行等待notify_one
//上面假设notify_one执行了唤醒的操作,那么第二个参数里面的list就不为空了,会返回true,
//返回true之后也就意味着不堵塞了,继续执行以下的代码
command = msgRecvQueue.front();
cout << "弹出来的数据是" << command << endl;
msgRecvQueue.pop_front();
}

变为:

    while (true)
{
unique_lock<mutex> sbg(my_mutex);
my_cond.wait(sbg, [this] {
if (!msgRecvQueue.empty())
return true;
else
return false;
});//第二个参数如果返回false,wait会解锁,并堵塞在这一行等待notify_one
//上面假设notify_one执行了唤醒的操作,那么第二个参数里面的list就不为空了,会返回true,
//返回true之后也就意味着不堵塞了,继续执行以下的代码 sbg.unlock();//把锁打开了
chrono::milliseconds dura(2000);
this_thread::sleep_for(dura);
command = msgRecvQueue.front();
cout << "弹出来的数据是" << command << endl;
msgRecvQueue.pop_front();
}

未完

condition_variable中的和wait_once和notify_one及notify_all实例代码的更多相关文章

  1. JAVA中使用FTPClient实现文件上传下载实例代码

    一.上传文件 原理就不介绍了,大家直接看代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...

  2. jquery中邮箱地址 URL网站地址正则验证实例代码

    QQ网站有一个网站举报的功能,看了一些js代码觉得写得很不错,我就拿下来了,下面是一个email验证与url网址验证js代码,分享给大家 email地址验证 复制代码代码如下: function ch ...

  3. YbSoftwareFactory 代码生成插件【二十五】:Razor视图中以全局方式调用后台方法输出页面代码的三种方法

    上一篇介绍了 MVC中实现动态自定义路由 的实现,本篇将介绍Razor视图中以全局方式调用后台方法输出页面代码的三种方法. 框架最新的升级实现了一个页面部件功能,其实就是通过后台方法查询数据库内容,把 ...

  4. C#开发中使用Npoi操作excel实例代码

    C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Exce ...

  5. @有两个含义:1,在参数里,以表明该变量为伪参数 ,在本例中下文里将用@name变量代入当前代码中2,在字串中,@的意思就是后面的字串以它原本的含义显示,如果不

    @有两个含义:1,在参数里,以表明该变量为伪参数 ,在本例中下文里将用@name变量代入当前代码中 2,在字串中,@的意思就是后面的字串以它原本的含义显示,如果不加@那么需要用一些转义符\来显示一些特 ...

  6. 关于JAVA中事件分发和监听机制实现的代码实例-绝对原创实用

    http://blog.csdn.net/5iasp/article/details/37054171 文章标题:关于JAVA中事件分发和监听机制实现的代码实例 文章地址: http://blog.c ...

  7. C#中的快捷键,可以更方便的编写代码 (转载)

    C#中的快捷键,可以更方便的编写代码 CTRL + SHIFT + B 生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL + SHIFT + O 打开项目 CTRL + ...

  8. C# WinForm中 让控件全屏显示的实现代码

    夏荣全 ( lyout(at)163.com )原文 C#中让控件全屏显示的实现代码(WinForm) 有时候需要让窗口中某一块的内容全屏显示,比如视频播放.地图等等.经过摸索,暂时发现两种可行方法, ...

  9. asp.net中生成缩略图并添加版权实例代码

    这篇文章介绍了asp.net中生成缩略图并添加版权实例代码,有需要的朋友可以参考一下 复制代码代码如下: //定义image类的对象 Drawing.Image image,newimage; //图 ...

随机推荐

  1. VUE3 之 样式绑定

    1. 概述 老话说的好:脚踏实地,从小事做起. 言归正传,今天我们来聊聊 VUE3 的样式绑定. 2. 样式绑定 2.1 样式例子 <style> /* 颜色 */ .color-red ...

  2. velocity使用foreach进行遍历时$velocityCount不起作用

    把$velocityCount替换成$foreach.count 例如 #if($foreach.count != $columns.size()),#end

  3. JAVA通过正则匹配html里面body标签的内容,去掉body标签

    /** * 获取html中body的内容 包含body标签 * @param htmlStr html代码 * @return */ public static String getBody(Stri ...

  4. JAVA中JDK1.8的LocalDateTime日期类的操作方法

    LocalDateTime与Date相互转换参考:https://www.cnblogs.com/pxblog/p/13745972.html 关键类 Instant:瞬时时间. LocalDate: ...

  5. 【九度OJ】题目1176:树查找 解题报告

    [九度OJ]题目1176:树查找 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1176 题目描述: 有一棵树,输出某一深度的所有节点 ...

  6. 【LeetCode】475. Heaters 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...

  7. 【LeetCode】809. Expressive Words 解题报告(Python)

    [LeetCode]809. Expressive Words 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  8. Shell实现文件内容批量替换的方法

    在Linux系统中,文件内容的批量替换同Windows平台相比要麻烦一点.不过这里可以通过Shell命令或脚本的方式实现批量替换的功能. 笔者使用过两个命令:perl和sed ,接下来会对其做出说明. ...

  9. MySQL 中 count(*) 和 count(1)

    一张有 100W 条数据的表 CREATE TABLE `user` (  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,  `username` var ...

  10. Jenkins安装教程:Windows环境通过jenkins.war安装

    1.Windows操作系统下,安装jdk.tomcat.maven.git,并配置好对应的环境变量,安装教程请自行查询资料 2.将下载的jenkins.war放入到tomcat的webapp文件夹下, ...