boost锁机制中Semaphores的介绍

结构

  • 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/条件组合更有效率。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/446846.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android Gradle 批量修改生成的apk文件名

目录一、简介二、代码实现1、 Gradle 3.0以下版本2、Gradle 3.0以上版本一、简介 平时开发都知道&#xff0c;我们要上线的时候需要在Android studio打包apk文件&#xff0c;可是默认的打包名是app-release.apk或者app-debug.apk这样的名字&#xff0c;太没有辨识度了。 下面…

C++boost Class named_condition翻译

Class named_condition boost::interprocess::named_condition 简介 // In header: <boost/interprocess/sync/named_condition.hpp>class named_condition { public:// construct/copy/destructnamed_condition(create_only_t, const char *, const permissions &…

Android Studio 代理设置以及代理完全清除

目录一、代理设置二、代理完全清除一、代理设置 首先我们来看下怎样设置代理&#xff0c;Mac下打开【Preferences…】&#xff0c;然后搜索"HTTP"&#xff0c;选择【HTTP Proxy】&#xff0c;按图中设置配置好后&#xff0c;点击【Apply】&#xff0c;然后在点击【O…

安卓布局位置,dp与px的区别

手机6寸—指对角线 布局位置 横轴—x轴 纵轴—y轴 一个像素点 dp与Px dp:设备无关像素,与像素密度相关,像素距离 dpi:像素密度,每英寸包含的像素数 px:屏幕上一个物理像素点 ldpi低密度 1dp0.75px mdpi中密度 1dp1px hdpi高密度 1dp1.5px xhdpi超高密度 1dp2px xxhdpi超…

Android Studio 快捷键大全(Mac系统)

目录一、Mac上的按键符号二、快捷键查找/查看相关控制操作相关代码重构相关一、Mac上的按键符号 符号说明⌥option / alt⇧shift⌃control⌘command⎋esc 二、快捷键 查找/查看相关 快捷键说明双击 shift搜索任意内容command F / command R当前文件查找/替换&#xff0c;使…

ubuntu下clion软件连接boost库文件

整体配置 cmake_minimum_required(VERSION 3.17) project(mutex_learn)set(CMAKE_CXX_STANDARD 14) #boost库所在的根目录set(BOOST_ROOT "/usr/local/include/boost") #添加头文件搜索路径 include_directories(/usr/local/include) #添加库文件搜索路径 link_dir…

Android程序结构

Project方式 .gradle文件夹:编译相关生成 .idea文件夹:idea生成 app文件夹----应用程序的源代码和资源 build----编译后的文件存放的位置,最终apk文件存放的位置 libs:存放.jar和.so文件 src:AndroidTest与test存放测试相关的内容 main中Java文件夹存放Java源码,res文件…

通过Github创建Android库供其他项目依赖引用

目录一、简介二、实现第一步&#xff1a;将自己的项目托管到Github上。第二步&#xff1a;点击releases。第三步&#xff1a;创建release。第四步&#xff1a;填写版本号、名称、描述信息。第五步&#xff1a;点击【Publish release】。第六步&#xff1a;复制项目路径。第七步…

使用boost模板函数实现读写锁

介绍 shared_mutex即读写锁&#xff0c;不同与我们常用的独占式锁mutex&#xff0c;shared_mutex是共享与独占共存的锁&#xff0c;实现了读写锁的机制&#xff0c;即多个读线程一个写线程&#xff0c;通常用于对于一个共享区域的读操作比较频繁&#xff0c;而写操作比较少的情…

安卓内边距padding与外边距magrin

内边距padding与外边距margin 内边距只有容器才有,即里面要有视图 具体示例

Android Studio发布项目到jcenter

目录一、创建Bintray账户及Maven仓库二、上传项目到jcenter1、配置Android项目2、Gradle命令上传3、在项目中引用4、Add to JCenter三、Demo示例一、创建Bintray账户及Maven仓库 1、打开Bintray首页&#xff0c;点击 For an Open Source Account &#xff0c;快速注册或者用第…

C++读取文件,将文件内容读到string字符串里面

使用stringstream和ifstream实现 代码 std::ifstream f{file_name, std::ios::binary};std::stringstream ss;ss << f.rdbuf();auto data ss.str();

Android MotionEvent中getX()、getRawX()和getTop()的区别

为了展示getX()、getRawX()和getTop()方法的区别&#xff0c;我们写了一个简单的测试Demo&#xff0c;我们写了一个自定义控件&#xff08;继承ImageView&#xff09;。 package com.demo.ui;import android.content.Context; import android.support.annotation.Nullable; im…

C++常量

常量 1.字面常量与符号常量 字面常量:从字面形式可以识别的常量 eg:1.2;‘A’ 整型常量:八进制(以0开头),十六进制(以0x或0X开头) 浮点型常量: 小数形式(整数和小数可以省略其中之一------为0时) eg:.123(0.123) 123.(123.0) 指数形式 0.23e1(0.2310^1) 0.23E-2(0.2310^-2)…

Synchronization 进程锁

Boost.Interprocess允许多个进程同时使用共享内存。因为共享内存从定义上来说是进程间共享的&#xff0c;所以Boost.Interprocess需要支持某种同步。想到同步&#xff0c;我们会想到C11标准库中的类或Boost.Thread。但是这些类只能用来同步同一进程内的线程&#xff0c;它们不支…

Android 获取屏幕宽度和高度的几种方法

方法一&#xff1a; public static void getScreenSize1(Context context){WindowManager windowManager (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);Display defaultDisplay windowManager.getDefaultDisplay();Point point new Point();defaultD…

毛概 第二章新民主主义革命理论

1.新民主主义革命的总路线 2.

解决undefined reference to symbol ‘sem_close@@GLIBC_2.2.5‘问题

错误图示 问题原因 编译的时候&#xff0c;没有引入库文件 sem()位于pthread库中&#xff0c;所以在编译和链接时请确保使用-pthread标志&#xff0c;因此在编译的时候需要导入pthread库文件编译的顺序出现问题 解决办法 在clion的CMakeLists.txt文件中添加这一行代码set(CM…

Android 在onCreate()方法中获取控件宽高值为0解决方案

大家很多时候需要在Activity或者Fragment的onCreate()方法中获取声明的空间的高度或者宽度&#xff0c;进行位置移动或者其他操作&#xff0c;但是当调用 view.getHeight() 或者 view.getWidth() 获取的竟然为0。。。 其实很容易理解&#xff0c;在onCreate()的时候&#xff0…