condition_variable中的和wait_once和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;
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实例代码的更多相关文章
- 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 ...
- jquery中邮箱地址 URL网站地址正则验证实例代码
QQ网站有一个网站举报的功能,看了一些js代码觉得写得很不错,我就拿下来了,下面是一个email验证与url网址验证js代码,分享给大家 email地址验证 复制代码代码如下: function ch ...
- YbSoftwareFactory 代码生成插件【二十五】:Razor视图中以全局方式调用后台方法输出页面代码的三种方法
上一篇介绍了 MVC中实现动态自定义路由 的实现,本篇将介绍Razor视图中以全局方式调用后台方法输出页面代码的三种方法. 框架最新的升级实现了一个页面部件功能,其实就是通过后台方法查询数据库内容,把 ...
- C#开发中使用Npoi操作excel实例代码
C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Exce ...
- @有两个含义:1,在参数里,以表明该变量为伪参数 ,在本例中下文里将用@name变量代入当前代码中2,在字串中,@的意思就是后面的字串以它原本的含义显示,如果不
@有两个含义:1,在参数里,以表明该变量为伪参数 ,在本例中下文里将用@name变量代入当前代码中 2,在字串中,@的意思就是后面的字串以它原本的含义显示,如果不加@那么需要用一些转义符\来显示一些特 ...
- 关于JAVA中事件分发和监听机制实现的代码实例-绝对原创实用
http://blog.csdn.net/5iasp/article/details/37054171 文章标题:关于JAVA中事件分发和监听机制实现的代码实例 文章地址: http://blog.c ...
- C#中的快捷键,可以更方便的编写代码 (转载)
C#中的快捷键,可以更方便的编写代码 CTRL + SHIFT + B 生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL + SHIFT + O 打开项目 CTRL + ...
- C# WinForm中 让控件全屏显示的实现代码
夏荣全 ( lyout(at)163.com )原文 C#中让控件全屏显示的实现代码(WinForm) 有时候需要让窗口中某一块的内容全屏显示,比如视频播放.地图等等.经过摸索,暂时发现两种可行方法, ...
- asp.net中生成缩略图并添加版权实例代码
这篇文章介绍了asp.net中生成缩略图并添加版权实例代码,有需要的朋友可以参考一下 复制代码代码如下: //定义image类的对象 Drawing.Image image,newimage; //图 ...
随机推荐
- 在mysql5.8中用json_extract函数解析json
背景:某个字段的数据中是JSON,需要提取其中的卡号部分,如: {"objType":"WARE","orderId":6771254073 ...
- Pointcut 表达式
AOP 概念篇 今天介绍 Pointcut 的表达式 通配符 常见的通配符如下 .. 含义一:方法表达式中.代表任意数量的参数 @Service public class HelloService { ...
- C++之递归遍历数组
倒序输出 源码 void print_arr_desc(int arr[], unsigned int len) { if (len) { std::cout << "a[&qu ...
- nim_duilib(15)之duilib属性列表.xml
Note 为了更加方便查看duilib的属性(github有时候打不开),特此记录. 阅读本文,可以知道控件有哪些属性,可以写在xml文件中.个别需要结合源码一起看 from here 原文 < ...
- Pikachu漏洞练习-SQL-inject(二)
- Java锁与非阻塞算法的性能比较与分析+原子变量类的应用
15.原子变量与非阻塞同步机制 在java.util.concurrent包中的许多类,比如Semaphore和ConcurrentLinkedQueue,都提供了比使用Synchronized更好的 ...
- [linux]ubuntu18.04 屏幕分辨率不适应问题
今天换了新显示器,发现更大的屏幕不适应原有的屏幕分辨率,看起来特别变扭. 在设置处查看最高分辨率仅为1024*748,没有与屏幕相适应的1920*1080(16:9). 解决方式: 1. 终端输入命令 ...
- 云南农职 - 互联网技术学院 - 美和易思大一SCME JAVA高级结业考试机试试题
目录 一.语言和环境 二.实现功能 1.文件复制功能(IO) 2.消息接受站建设 三.评分标准 四.实现代码 一.语言和环境 实现语言:Java. 开发工具:eclipse. 使用技术:IO流+网络编 ...
- 编写Java程序,在电脑硬盘里,查看 f:\text4\name 目录是否存在。
查看本章节 查看作业目录 需求说明: 在电脑硬盘里,查看 f:\text4\name 目录是否存在.如果不存在,则创建该目录:如果存在,则查找 readme.txt文件是否存在.如果 readme.t ...
- MySQL数据库常用命令汇总
-- 查看mysql的当前登陆用户 select user(); -- 列出数据库 show databases; -- 使用数据库 use mysql; describe mysql.user; s ...