结构

  • What's A Semaphore?
  • Boost.Interprocess Semaphore Types And Headers
  • Anonymous semaphore example

What's A Semaphore?

  • 旗语是一种基于内部计数的进程间同步机制,它提供了两种基本操作。
  • 等待:测试旗语数的值,如果小于或等于0,则等待,否则递减旗语数。
  • Post:增加旗语数。增加信号量 如果有进程被阻塞,则唤醒其中一个进程。
  • 如果初始旗语数被初始化为1,则Wait操作相当于mutex锁定,Post相当于mutex解锁。这种类型的semaphore被称为二进制semaphore。
  • 虽然旗语可以像mutexes一样使用,但它们有一个独特的特点:与mutexes不同,Post操作不需要由执行Wait操作的同一个线程/进程执行。

Boost.Interprocess Semaphore Types And Headers

  • Boost.Interprocess offers the following semaphore types:
  • #include <boost/interprocess/sync/interprocess_semaphore.hpp>
  • interprocess_semaphore: An anonymous semaphore that can be placed in shared memory or memory mapped files.
  • interprocess_semaphore。一个匿名的信号体,可以放在共享内存或内存映射文件中。
  • #include <boost/interprocess/sync/named_semaphore.hpp>
  • named_semaphore: A named semaphore.
  • named_semaphore。一个命名的旗语。

Anonymous semaphore example

  • We will implement an integer array in shared memory that will be used to transfer data from one process to another process. The first process will write some integers to the array and the process will block if the array is full.
  • The second process will copy the transmitted data to its own buffer, blocking if there is no new data in the buffer.
  • This is the shared integer array (doc_anonymous_semaphore_shared_data.hpp):
  • 我们将在共享内存中实现一个整数数组,用于从一个进程向另一个进程传输数据。第一个进程将向数组写入一些整数,如果数组已满,该进程将阻塞。
  • 第二个进程将把传输的数据复制到自己的缓冲区,如果缓冲区中没有新的数据,则阻塞。
  • 这就是共享整数数组(doc_anonymous_semaphore_shared_data.hpp)。
#include <boost/interprocess/sync/interprocess_semaphore.hpp>struct shared_memory_buffer
{enum { NumItems = 10 };shared_memory_buffer(): mutex(1), nempty(NumItems), nstored(0){}//Semaphores to protect and synchronize accessboost::interprocess::interprocess_semaphoremutex, nempty, nstored;//Items to fillint items[NumItems];
};
  • 这是进程主进程。创建共享内存,将整数数组放置在那里,并逐个开始整数,如果数组满了,则阻塞。
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
#include "doc_anonymous_semaphore_shared_data.hpp"using namespace boost::interprocess;int main ()
{//Remove shared memory on construction and destructionstruct 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);//Set sizeshm.truncate(sizeof(shared_memory_buffer));//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 memoryshared_memory_buffer * data = new (addr) shared_memory_buffer;const int NumMsg = 100;//Insert data in the arrayfor(int i = 0; i < NumMsg; ++i){data->nempty.wait();data->mutex.wait();data->items[i % shared_memory_buffer::NumItems] = i;data->mutex.post();data->nstored.post();}return 0;
}

The second process opens the shared memory and copies the received integers to it's own buffer:

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
#include "doc_anonymous_semaphore_shared_data.hpp"using namespace boost::interprocess;int main ()
{//Remove shared memory on destructionstruct shm_remove{~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }} remover;//Create a shared memory object.shared_memory_object shm(open_only                    //only create,"MySharedMemory"              //name,read_write  //read-write mode);//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 the shared structureshared_memory_buffer * data = static_cast<shared_memory_buffer*>(addr);const int NumMsg = 100;int extracted_data [NumMsg];//Extract the datafor(int i = 0; i < NumMsg; ++i){data->nstored.wait();data->mutex.wait();extracted_data[i] = data->items[i % shared_memory_buffer::NumItems];data->mutex.post();data->nempty.post();}return 0;
}
  • The same interprocess communication can be achieved with a condition variables and mutexes, but for several synchronization patterns, a semaphore is more efficient than a mutex/condition combination.
  • 同样的进程间通信可以用条件变量和mutexes来实现,但对于几种同步模式,semaphore比mutex/条件组合更有效率。

boost锁机制中Semaphores的介绍相关推荐

  1. aba问题mysql_解决CAS机制中ABA问题的AtomicStampedReference详解

    AtomicStampedReference是一个带有时间戳的对象引用,能很好的解决CAS机制中的ABA问题,这篇文章将通过案例对其介绍分析. 一.ABA问题 ABA问题是CAS机制中出现的一个问题, ...

  2. Android版本的Wannacry文件加密病毒样本分析(附带锁机)

    一.前言 之前一个Wannacry病毒样本在PC端肆意了很久,就是RSA加密文件,勒索钱财.不给钱就删除.但是现在移动设备如此之多,就有一些不法分子想把这个病毒扩散到移动设备了,这几天一个哥们给了一个 ...

  3. Android 程序保活,锁机代码

    前言 保活:如何让我们的app在Android系统不被杀死 保证存活,简单做法就是提升程序的优先级,看完本文一些流氓锁机你也会了哦.但锁机源码我不打算提供 为了防止某些恶心的人直接复制然后在市面上搞破 ...

  4. mysql数据刷盘_MySQL InnoDB 日志管理机制中的MTR和日志刷盘

    1.MTR(mini-transaction) 在MySQL的 InnoDB日志管理机制中,有一个很重要的概念就是MTR.MTR是InnoDB存储擎中一个很重要的用来保证物理写的完整性和持久性的机制. ...

  5. Neutrino追问AMA第20期 | Ultrain 郭睿:在RPoS共识机制中,核心是随机数和 BFT 算法

    在4月10日晚举行第的19期 Neutrino 追问 AMA中 ,我们邀请到了 Ultrain 联合创始人& CEO 郭睿.在社群交流中,郭睿表示,创新的共识机制随机可信证明机制(R-PoS) ...

  6. Android版本的 Wannacry 文件加密病毒样本分析 附带锁机

    一.前言 之前一个Wannacry病毒样本在PC端肆意了很久,就是RSA加密文件,勒索钱财.不给钱就删除.但是现在移动设备如此之多,就有一些不法分子想把这个病毒扩散到移动设备了,这几天一个哥们给了一个 ...

  7. 疯壳AI开源无人机地面站上位机的使用和介绍

    地面站上位机使用和介绍 COCOFLY地面站上位机是配套COCOFLY无人机使用的,该地面站上位机功能非常的全.主要分为5大功能板块,分别是基本收发.飞控设置.波形显示.图形编队以及飞控状态. 1.1 ...

  8. Android锁机样本分析

    样本基本信息 样本包名:Android.qun.zhu.an.zhuo.kou.kou MD5值: 36f2db49dcb62247055df771dca47bde 来源:52破解论坛某求助贴. 样本 ...

  9. 013 Android锁机病毒分析

    文章目录 免流服务器-锁机病毒分析 秒抢红包-锁机病毒分析 免流服务器-锁机病毒分析 首先来分析这个免流服务器的锁机病毒,文件信息如下 文件: 免流服务器.apk 大小: 799835 bytes 修 ...

最新文章

  1. 《C++ Primer》14.3.1节练习
  2. C语言定义:__DATE__和_TIME__
  3. MapReduce高级编程
  4. VGA接口一根针折了
  5. [翻译]Web开发牛人访谈:你们都在用什么?
  6. java服务器向客户端发消息_socket 服务器向指定的客户端发消息
  7. 【vue】生成条形码
  8. 梦幻手游显示连接服务器,?梦幻西游手游无法连接服务器怎么办 无法访问服务器解决方法?...
  9. Wrong namespace. Expected 'com.example.springboot.mapper.DepartmentMapper' but found 'com.sandystar.
  10. jQuery双指放大缩小页面内容
  11. python学习(列表,元祖)
  12. vs code 保存显示无法写入文件的解决方法
  13. [CF1129E]Legendary Tree
  14. Nature综述:未培养微生物的新兴培养技术
  15. asp毕业设计——基于asp+access的学生成绩查询系统设计与实现(毕业论文+程序源码)——成绩查询系统
  16. 9.Unity2D 横版 简单AI 之 敌人跳跃条件优化+自动范围内检测敌人发起攻击(索敌)+对象池优化+主角受伤死亡
  17. ASIL-汽车安全完整性等级介绍
  18. 关于74HC374使用的总结
  19. 梳理谷歌45篇入选CVPR论文,后附GAN主题演讲PPT下载!
  20. 数值分析-牛顿-柯特斯公式的概念、推导与应用

热门文章

  1. Spring笔记——数据源配置
  2. 【转】交织容积重建技术:基本原理与临床价值
  3. 【转】Ubuntu 16.04 Nvidia驱动安装(run方式)
  4. 【转】医学图像之DICOM格式解析
  5. 【转】微软Azure Functions使用入门
  6. ABP入门系列(2)——领域层创建实体
  7. 东北师范大学计算机学院的导师,东北师范大学计算机科学与信息技术学院研究生导师简介-王佳男...
  8. 【Gym - 101915D】Largest Group(二分图最大团,状压dp)
  9. 【牛客 - 696C】小w的禁忌与小G的长诗(dp 或 推公式容斥)
  10. 【CH - 1401】 兔子与兔子(字符串哈希)