// 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. C笔试题:将int型数组强制转换为char*,再求strlen,涉及大小端

    1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 int a[2000]; 6 char *p = (ch ...

  2. c++11之 algorithm 算法库新增 minmax_element同时计算最大值和最小值

    0.时刻提醒自己 Note: vector的释放 1. minmax_element 功能 寻找范围 [first, last) 中最小和最大的元素. 2. 头文件 #include <algo ...

  3. CMake之常用内置变量和message用法

    关于 cmake 定义了相当丰富的变量,然而,我常用的也就那几个 脑子笨,记不住变量的值时,我会使用cmake的message函数输出变量值 为什么要写这个? 最近尝试使用Modern CMake, ...

  4. 【LeetCode】87. Scramble String 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 动态规划 日期 题目地址:https://le ...

  5. 【LeetCode】558. Quad Tree Intersection 解题报告(Python)

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

  6. 低成本CH7511芯片方案|CH7511电路设计参考|CS5211替代CH7511

    CH7511是主要用于设计eDP转LVDS转换器,怎么样设计一款低成本低BOM简单的DP转LVDS的转接设置,目前有一款可以替代兼容CH7511的方案电路,并且其设计电路整体BOM成本较低,并且设计简 ...

  7. MySQL数据操作与查询笔记 • 【第3章 DDL 和 DML】

    全部章节   >>>> 本章目录 3.1 使用 DDL 定义数据库表结构 3.1.1 SQL 简介 3.1.2 维护数据库和创建数据表 3.2 使用 DDL 维护数据库表结构 ...

  8. 引用element-ui的Drawer抽屉组件报错问题

    前提:vue项目采取按需引入的方式引入element,并且使用其他组件都正常,没有发生异常 问题表现: 在vue项目中引用了Drawer 抽屉组件,结果报错 意思就是组件未注册,当时我的表情: 没办法 ...

  9. 【操作系统】I/O多路复用 select poll epoll

    @ 目录 I/O模式 I/O多路复用 select poll epoll 事件触发模式 I/O模式 阻塞I/O 非阻塞I/O I/O多路复用 信号驱动I/O 异步I/O I/O多路复用 I/O 多路复 ...

  10. 最简短的 AC 自动机!

    写在前面 仍然是写给自己的,看不懂别怪我. 最简短的 AC 自动机! AC 自动机用于多模匹配. 模式串被插入一个添加了一些转移边的 Trie 中.在匹配的时候,若失配,则使下一个字符跳到该节点的 f ...