使用BOOST::Interprocess完成内存共享与同步
来源:互联网 发布:sexlab 知乎 编辑:程序博客网 时间:2024/06/02 04:27
http://blog.csdn.net/turner_gao/article/details/51914848
http://blog.csdn.net/great3779/article/details/7226388
http://blog.csdn.net/great3779/article/details/7240271
http://blog.chinaunix.net/uid-28595538-id-5068065.html
匿名互斥量举例
想象两个进程需要往一个共享内存中的循环缓冲区中写入轨迹。每个进程都需要排他地访问这个循环缓冲区,并将轨迹写入,然后继续运行。
为了保护循环缓冲区,我们可以保存一个进程共享的互斥量在这里面。每个进程将在写数据之前锁定该互斥量,并且在写入轨迹后,写入一个标志。
定义一个头文件doc_anonymous_mutex_shared_data.hpp:
#include <boost/interprocess/sync/interprocess_mutex.hpp>struct shared_memory_log{enum { NumItems = 100 };enum { LineSize = 100 };shared_memory_log(): current_line(0),end_a(false),end_b(false){}boost::interprocess::interprocess_mutex mutex;char items[NumItems][LineSize];int current_line;bool end_a;bool end_b;}
下面是主进程,创建共享内存,构造循环缓冲区和写轨迹:
#include <boost/interprocess/shared_memory_object.hpp>#include <boost/interprocess/mapped_region.hpp>#include <boost/interprocess/sync/scoped_lock.hpp>#include “doc_anonymous_mutex_shared_data.hpp”#include <iostream>#include <cstdio>using namespace boost::interprocess;int main(){ try{ struct shm_remove { shm_remove() { shared_memory_object::remove(“MySharedMemory”);} ~shm_remove() { shared_memory_object::remove(“MySharedMemory”);} } remover; shared_memory_object shm ( create_only ,”MySharedMemory” ,read_write ); shm.truncate(sizeof(shared_memory_log)); mapped_region region( shm,read_write);//类封装时作为成员变量 void * addr = region.get_address(); shared_memory_log * data = new (addr) shared_memory_log; for( int i=0;i<shared_memory_log::NumItems;++i ){ scoped_lock<interprocess_mutex> lock(data->mutex); std::sprintf( data->items[(data->current_line++) % shared_memory_log::NumItems],”%s_%d”,”process_a”,i); if( i==(shared_memory_log::NumItems-1) ) data->end_a = true; } // 等待另外一个进程结束 while( 1 ){ scoped_lock<interprocess_mutex> lock(data->mutex); if( data->end_b) break; }}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 “doc_anonymous_mutex_shared_data.hpp”#include <iostream>#include <cstdio>using namespace boost::interprocess;int main(){ struct shm_remove{ ~shm_remove(){ shared_memory_object::remove(“MySharedMemory”);}} remover;shared_memory_object shm( open_only,”MySharedMemory”,read_write );mapped_region region( shm,read_write );//类封装时作为成员变量void * addr = region.get_address();shared_memory_log * data = static_cast<shared_memory_log*>(addr);for( int i=0;i<100;++i ){ scoped_lock<interprocess_mutex> lock(data->mutex); std::sprintf(data->items[(data->current_line++)%shared_memory_log::NumItems] ,”process_b_%d”,i); if( i==(shared_memory_log::NumItems-1)) data->end_b = true;}while(1){scoped_lock<interprocess_mutex> lock(data->mutex);if( data->end_a)break;}return 0;}
正如我们看到的,互斥量在保护数据上非常实用,
类封装时,共享内存谁创建谁删除:
CShareMem::CShareMem(int iPlatForm/*=PLATE_SERVER*/):pShared_memory_data(NULL),iRegionSize(0),m_regionSer(NULL),m_regionCli(NULL),m_plateForm(iPlatForm){ switch (iPlatForm) { case PLATE_SERVER: shared_memory_object::remove("SharedMemory"); break; default: break; }}CShareMem::~CShareMem(void){ switch (m_plateForm) { case PLATE_SERVER: shared_memory_object::remove("SharedMemory"); break; default: break; }}
阅读全文
0 0
- 使用BOOST::Interprocess完成内存共享与同步
- Boost IPC(interprocess)实现内存共享
- 【Boost】Interprocess - 共享内存、文件映射介绍
- boost interprocess使用
- Boost.Interprocess使用手册翻译之四:在进程间共享内存 (Sharing memory between processes)
- Boost.Interprocess使用手册翻译之四:在进程间共享内存 (Sharing memory between processes)
- boost之Interprocess库,进程间通讯库之共享内存
- Boost.Interprocess使用手册翻译之四:在进程间共享内存 (Sharing memory between processes)
- Boost.Interprocess使用手册翻译之四:在进程间共享内存 (Sharing memory between processes)
- Boost.Interprocess使用手册翻译之四:在进程间共享内存 (Sharing memory between processes)
- boost::interprocess 内存映射文件的用法
- 共享内存与同步线程的使用例子
- boost::interprocess 内存映射文件代替 fstream用法
- 共享内存与Thread的同步
- Linux共享内存示例,使用信号量同步
- Boost.Interprocess翻译-简介
- boost.interprocess翻译
- Boost.Interprocess使用指南(1)
- 建造者模式
- 51nod 1442 士兵的旅行(最大流)
- Java_日历生成(Lab 4_Exercise 5)
- windwsserver 2008+IIS7发布web拒绝访问
- 第三章 求100以内的素数
- 使用BOOST::Interprocess完成内存共享与同步
- bzoj1087[SCOI2005][互不侵犯King] 状压DP
- 降低Face ID精度提高手机产量?苹果:完全没这事 | 行业
- 性爱机器人发明者:我打算与机器人生个“孩子” | 聚焦
- 学习Linux C编程之线性表
- CRC、MD5、SHA1 有何区别?
- 让AI达到人类水平?首先需要翻越这“五座大山” | 讨论
- 到了2040年 你在这些国家是买不到非电动车的! | 精选
- Java多线程编程-(13)- 关于锁优化的几点建议