对于boost锁机制结论性的介绍

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;
}

 

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

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

相关文章

C++数据类型

C 数据类型 |–基本数据类型: 整型 short短整型 2 int基本整型4 long长整型 8 浮点型 float单精度型 4 double双精度型 8 long double长双精度型 16 字符型 char 1 逻辑型 bool 1 空类型 void |–构造类型 数组类型 构造体类型 struct 共用体类型 union 枚举类型 enum 类类型…

Android Gradle 多渠道打包、动态配置AppName

目录一、简介二、Gradle多渠道打包1、普通做法2、Gradle多渠道打包一、简介 因为国内Android应用分发市场的现状&#xff0c;我们在发布APP时&#xff0c;一般需要生成多个渠道包&#xff0c;上传到不同的应用市场。这些渠道包需要包含不同的渠道信息&#xff0c;在APP和后台交…

boost锁机制中Semaphores的介绍

结构 Whats A Semaphore?Boost.Interprocess Semaphore Types And HeadersAnonymous semaphore example Whats A Semaphore? 旗语是一种基于内部计数的进程间同步机制&#xff0c;它提供了两种基本操作。等待&#xff1a;测试旗语数的值&#xff0c;如果小于或等于0&#x…

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…