Conditions

  • What's A Condition Variable?
  • Boost.Interprocess Condition Types And Headers
  • Anonymous condition example

What's A Condition Variable?

  • 在前面的例子中,一个mutex被用来锁定,但我们不能用它来有效地等待,直到满足继续的条件。一个条件变量可以做两件事。
  • wait: 线程被阻塞,直到其他线程通知它可以继续,因为导致等待的条件已经消失。
  • 通知:线程通知其他线程可以继续。线程向一个被阻塞的线程或所有被阻塞的线程发送一个信号,告诉它们引起它们等待的条件已经消失。
  • 条件变量中的等待总是与一个mutex相关联。在等待条件变量之前,必须先锁定该mutex。当在条件变量上等待时,线程解锁mutex并原子性地等待。
  • 当线程从等待函数中返回时(例如因为信号或超时),mutex对象再次被锁定。

Boost.Interprocess Condition Types And Headers

  • Boost.Interprocess offers the following condition types:
  • #include <boost/interprocess/sync/interprocess_condition.hpp>
  • interprocess_condition。一个匿名的条件变量,可以放在共享内存或内存映射文件中,与 boost::interprocess::interprocess_mutex 一起使用。
  • #include <boost/interprocess/sync/interprocess_condition_any.hpp>
  • interprocess_condition_any.一个匿名的条件变量,可以放在共享内存或内存映射文件中,用于任何锁类型。一个匿名的条件变量,可以放在共享内存或内存映射文件中,用于任何锁类型。
  • #include <boost/interprocess/sync/named_condition.hpp>
  • named_condition.一个命名的条件变量,与 named_mutex 一起使用。与named_mutex一起使用的命名条件变量。
  • #include <boost/interprocess/sync/named_condition_any.hpp>
  • named_condition: 一个命名的条件变量,可用于任何类型的锁。
  • 命名条件与匿名条件类似,但它们与命名的mutexes结合使用。好几次,我们不想用同步数据来存储同步对象。我们希望使用相同的数据改变同步方法(从进程间到进程内,或者没有任何同步)。将进程共享的匿名同步与同步数据一起存储将禁止这样做。我们希望通过网络或其他通信方式发送同步数据。发送过程共享的同步对象就没有任何意义了。

Anonymous condition example

  • 想象一下,一个进程,将一个跟踪写到一个简单的共享内存缓冲区,另一个进程逐一打印。第一个进程写入跟踪并等待另一个进程打印数据。为了达到这个目的,我们可以使用两个条件变量:第一个条件变量用来阻止发送者,直到第二个进程打印信息,第二个条件变量用来阻止接收者,直到缓冲区有痕迹要打印。
  • 共享内存跟踪缓冲区(doc_anonymous_condition_shared_data.hpp)
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>struct trace_queue
{enum { LineSize = 100 };trace_queue():  message_in(false){}//Mutex to protect access to the queueboost::interprocess::interprocess_mutex      mutex;//Condition to wait when the queue is emptyboost::interprocess::interprocess_condition  cond_empty;//Condition to wait when the queue is fullboost::interprocess::interprocess_condition  cond_full;//Items to fillchar   items[LineSize];//Is there any messagebool message_in;
};
  • 这是该进程的主进程。创建共享内存,将缓冲区放置在那里,并开始逐一写入消息,直到写下 "最后一条消息",表示没有更多的消息要打印。
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <iostream>
#include <cstdio>
#include "doc_anonymous_condition_shared_data.hpp"using namespace boost::interprocess;int main ()
{//Erase previous shared memory and schedule erasure on exitstruct shm_remove{shm_remove() { shared_memory_object::remove("MySharedMemory"); }~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }} remover;//Create a shared memory object.shared_memory_object shm(create_only               //only create,"MySharedMemory"           //name,read_write                //read-write mode);try{//Set sizeshm.truncate(sizeof(trace_queue));//Map the whole shared memory in this processmapped_region region(shm                       //What to map,read_write //Map it as read-write);//Get the address of the mapped regionvoid * addr       = region.get_address();//Construct the shared structure in memorytrace_queue * data = new (addr) trace_queue;const int NumMsg = 100;for(int i = 0; i < NumMsg; ++i){scoped_lock<interprocess_mutex> lock(data->mutex);if(data->message_in){data->cond_full.wait(lock);}if(i == (NumMsg-1))std::sprintf(data->items, "%s", "last message");elsestd::sprintf(data->items, "%s_%d", "my_trace", i);//Notify to the other process that there is a messagedata->cond_empty.notify_one();//Mark message buffer as fulldata->message_in = true;}}catch(interprocess_exception &ex){std::cout << ex.what() << std::endl;return 1;}return 0;
}
  • 第二个过程打开共享内存,打印每一条消息,直到收到 "最后一条消息 "消息。
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <iostream>
#include <cstdio>
#include "doc_anonymous_condition_shared_data.hpp"using namespace boost::interprocess;int main ()
{//Erase previous shared memory and schedule erasure on exitstruct shm_remove{shm_remove() { shared_memory_object::remove("MySharedMemory"); }~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }} remover;//Create a shared memory object.shared_memory_object shm(create_only               //only create,"MySharedMemory"           //name,read_write                //read-write mode);try{//Set sizeshm.truncate(sizeof(trace_queue));//Map the whole shared memory in this processmapped_region region(shm                       //What to map,read_write //Map it as read-write);//Get the address of the mapped regionvoid * addr       = region.get_address();//Construct the shared structure in memorytrace_queue * data = new (addr) trace_queue;const int NumMsg = 100;for(int i = 0; i < NumMsg; ++i){scoped_lock<interprocess_mutex> lock(data->mutex);if(data->message_in){data->cond_full.wait(lock);}if(i == (NumMsg-1))std::sprintf(data->items, "%s", "last message");elsestd::sprintf(data->items, "%s_%d", "my_trace", i);//Notify to the other process that there is a messagedata->cond_empty.notify_one();//Mark message buffer as fulldata->message_in = true;}}catch(interprocess_exception &ex){std::cout << ex.what() << std::endl;return 1;}return 0;
}
  • 通过条件变量,一个进程如果不能继续工作就可以阻塞,当满足继续工作的条件时,另一个进程可以唤醒它。
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <iostream>
#include <cstring>
#include "doc_anonymous_condition_shared_data.hpp"using namespace boost::interprocess;int main ()
{//Create a shared memory object.shared_memory_object shm(open_only                    //only create,"MySharedMemory"              //name,read_write                   //read-write mode);try{//Map the whole shared memory in this processmapped_region region(shm                       //What to map,read_write //Map it as read-write);//Get the address of the mapped regionvoid * addr       = region.get_address();//Obtain a pointer to the shared structuretrace_queue * data = static_cast<trace_queue*>(addr);//Print messages until the other process marks the endbool end_loop = false;do{scoped_lock<interprocess_mutex> lock(data->mutex);if(!data->message_in){data->cond_empty.wait(lock);}if(std::strcmp(data->items, "last message") == 0){end_loop = true;}else{//Print the messagestd::cout << data->items << std::endl;//Notify the other process that the buffer is emptydata->message_in = false;data->cond_full.notify_one();}}while(!end_loop);}catch(interprocess_exception &ex){std::cout << ex.what() << std::endl;return 1;}return 0;
}

对于boost锁机制结论性的介绍相关推荐

  1. 第二十节: 深入理解并发机制以及解决方案(锁机制、EF自有机制、队列模式等)

    一. 理解并发机制 1. 什么是并发,并发与多线程有什么关系? ①. 先从广义上来说,或者从实际场景上来说. 高并发通常是海量用户同时访问(比如:12306买票.淘宝的双十一抢购),如果把一个用户看做 ...

  2. 锁机制(自旋锁-乐观锁-悲观锁)

    各种锁机制(主要介绍自旋锁) 1. 自旋锁 2. 悲观锁---Synchornized 2.1 偏向锁 2.1.1 为什么要引入偏向锁? 2.1.2 偏向锁原理和升级过程 2.2 轻量级锁 2.2.1 ...

  3. 数据库事务及锁机制介绍

    2019独角兽企业重金招聘Python工程师标准>>> 事务介绍 因为一直使用Spring这种声明式的事务管理,一直以为事务的主要作用是对一个业务方法中多次执行数据库操作的最终提交. ...

  4. Redis 学习笔记-NoSQL数据库 常用五大数据类型 Redis配置文件介绍 Redis的发布和订阅 Redis_事务_锁机制_秒杀 Redis应用问题解决 分布式锁

    1.NoSQL数据库 1.1 NoSQL数据库概述 NoSQL(NosQL = Not Only sQL ),意即"不仅仅是sQL",泛指非关系型的数据库.NoSQL不依赖业务逻辑 ...

  5. Linux内核中锁机制之完成量、互斥量

    在上一篇博文中笔者分析了关于信号量.读写信号量的使用及源码实现,接下来本篇博文将讨论有关完成量和互斥量的使用和一些经典问题. 八.完成量 下面讨论完成量的内容,首先需明确完成量表示为一个执行单元需要等 ...

  6. 数据库锁机制为什么很重要?

    前言 在座的朋友们,你们的时间够用吗?想要成为一个成功的人吗?如果你们都有这样的疑惑,那就保持一刻谦虚的心态,跟着罗老师学习时间管理吧! 毕竟时间管理大师是一个用户访问多个资源,今天咱们来讲讲当多个用 ...

  7. 一文带你了解 MySQL 中的各种锁机制!

    MySQL中的锁机制,按粒度分为行级锁,页级锁,表级锁,其中按用法还分为共享锁和排他锁. 行级锁 行级锁是Mysql中锁定粒度最细的一种锁,表示只针对当前操作的行进行加锁. 行级锁能大大减少数据库操作 ...

  8. 大厂面试官必问的Mysql锁机制

    前言 前几天有粉丝和我聊到他找工作面试大厂时被问的问题,因为现在疫情期间,找工作也特别难找.他说面试的题目也比较难,都偏向于一两年的工作经验的面试题. 他说在一面的时候被问到Mysql的面试题,索引那 ...

  9. MySQL InnoDB锁机制全面解析分享

    写在前面:在设计新零售供应链wms(仓库管理系统)库存模块时,为了防止并发情况对库存的影响,查阅了一些资料,对InnoDB锁机制有了更全面的了解,在此做出分享,如有疏漏望不吝指正,愿共同进步!(此篇为 ...

最新文章

  1. 解决list-style-type属性失效
  2. linux并发控制之原子操作
  3. 隔离级别(未提交读、提交读、可重复读、可串行化)、多版本并发控制、Next-Key Locks(Record Locks、Gap Locks)
  4. c# 委托和委托事件
  5. python掷骰子_掷骰子童芯派 python硬件编程(上传模式)
  6. 对《Java编程思想》读者的一点建议
  7. 易语言制作计算机按键指令,易语言键代码一览表
  8. SQL删除重复数据并只保留一条
  9. IDEA 代码格式化插件Save Actions
  10. 解决添加打印机print spooler打印服务自动关闭故障
  11. C和指针 第13章 高级指针话题 13.2 高级声明
  12. Kaggle账号注册
  13. python猜数字统计游戏次数_猜数字游戏的实现(Python3.5)
  14. Ambari——大数据平台的搭建利器(一)
  15. Recheck Cond filter IO\CPU放大 原理与优化CASE - 含 超级大表 不包含(反选) SQL优化
  16. Jmeter接口测试+压力测试
  17. 同步与异步通信的区别
  18. 倚天屠龙记君临天下_战斗系统
  19. 使用SQL Server管理数据表
  20. asp mysql 留言本_手把手教你用ASP制作留言本

热门文章

  1. java内部类外部类_Java内部类:如何在内部类中返回外部类对象
  2. 【转】医学影像调窗技术!!!!
  3. 【转】CANOPEN总线的相关问题点整理分享*****
  4. Php点击更换封面,JavaScript_js实现点击图片改变页面背景图的方法,本文实例讲述了js实现点击图 - phpStudy...
  5. java解析string_java读取文件内容为string字符串的方法
  6. 【JS 逆向百例】webpack 改写实战,G 某游戏 RSA 加密
  7. Python 中 if __name__ == '__main__': 的理解
  8. oracle行转列 case,Oracle 行转列总结 Case When,Decode,PIVOT 三种方式
  9. 【ZOJ - 3212 】K-Nice (构造)
  10. 关于C++里面使用set_union,set_intersections、set_merge、set_difference、set_symmetric_difference等函数的使用总结