struct shareDataEx : shareData
{
int index;
int total_size;
};
typedef managed_shared_memory::segment_manager segment_manager_t; //段管理器
typedef allocator<shareDataEx, segment_manager_t> mem_allocator; //定义基于shareDataEx类型的分配器
typedef deque<shareDataEx, mem_allocator> mem_queue; //创建deque基于boost::Interprocess::containers::deque,使用mem_allocator分配器
m_segment = new managed_shared_memory(open_or_create, getMemName(m_name + "ProcessMemPool9", pid).c_str(), **);
m_queue = m_segment->find_or_construct<mem_queue>(getMemName(m_name + "m_queue", pid).c_str())(mem_allocator(m_segment->get_segment_manager()));
mem_allocator(m_segment->get_segment_manager()好像是分配deque兼容的分配器,不是太懂,有时间看看下面这个例子:
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <string>
#include <cstdlib> //std::system using namespace boost::interprocess; //Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
//This allocator will allow placing containers in the segment
typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator; //Alias a vector that uses the previous STL-like allocator so that allocates
//its values from the segment
typedef vector<int, ShmemAllocator> MyVector; //Main function. For parent process argc == 1, for child process argc == 2
int main(int argc, char *argv[])
{
if(argc == ){ //Parent process
//Remove shared memory on construction and destruction
struct shm_remove
{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover; //Create a new segment with given name and size
managed_shared_memory segment(create_only, "MySharedMemory", ); //Initialize shared memory STL-compatible allocator
const ShmemAllocator alloc_inst (segment.get_segment_manager()); //Construct a vector named "MyVector" in shared memory with argument alloc_inst
MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst); for(int i = ; i < ; ++i) //Insert data in the vector
myvector->push_back(i); //Launch child process
std::string s(argv[]); s += " child ";
if( != std::system(s.c_str()))
return ; //Check child has destroyed the vector
if(segment.find<MyVector>("MyVector").first)
return ;
}
else{ //Child process
//Open the managed segment
managed_shared_memory segment(open_only, "MySharedMemory"); //Find the vector using the c-string name
MyVector *myvector = segment.find<MyVector>("MyVector").first; //Use vector in reverse order
std::sort(myvector->rbegin(), myvector->rend()); //When done, destroy the vector from the segment
segment.destroy<MyVector>("MyVector");
}
getchar();
return ;
};

boost::interprocess::managed_shared_memory(2)(std::deque)的更多相关文章

  1. boost::interprocess::managed_shared_memory(2)(std::string)

    #include <iostream> #include <boost/interprocess/managed_shared_memory.hpp> #include < ...

  2. boost::interprocess(1)

    发送端:#include <iostream> #include <windows.h> #include <string> using namespace std ...

  3. boost::interprocess::shared_memory_object(1)(基本类型)

    #include <iostream> #include <boost/interprocess/managed_shared_memory.hpp> struct pos2d ...

  4. boost信号量 boost::interprocess::interprocess_semaphore的用法

    使用方法首先给信号量初始化赋值,可以根据需要设定需要的值,之前在写项目的过程中用这个控制下载的线程个数. boost::interprocess::interprocess_semaphore m_s ...

  5. boost::interprocess(2)

    //doc_anonymous_mutex_shared_data.hpp #include <boost/interprocess/sync/interprocess_mutex.hpp> ...

  6. C++ std::deque

    std::deque template < class T, class Alloc = allocator > class deque; Double ended queue deque ...

  7. std::deque

    deque容器为一个给定类型的元素进行线性处理,像向量一样,它能够快速地随机访问任一个元素,并且能够高效地插入和删除容器的尾部元素.但它又与vector不同,deque支持高效插入和删除容器的头部元素 ...

  8. C++ std::deque 基本用法

    #include <iostream> #include <string> #include <deque> // https://zh.cppreference. ...

  9. Boost.Interprocess

    https://github.com/svebert/InterprocessMsg 好像消息队列

随机推荐

  1. java基础讲解10-----类的高级特性

    一.final关键字 1.final关键字修饰变量,表示变量不可以被改变,如果想修改,编译器不会接受的. 注意:final关键字定义的变量必须赋值 public  static final 修饰  白 ...

  2. Linux监控平台搭建

    Linux监控平台介绍 zabbix监控介绍 zabbix监控流程图 安装zabbix 准备两台主机: zabbix服务端:192.168.133.88 zabbix客户端:192.168.133.6 ...

  3. spring中aop以xml配置方式

    1 引jar包 springAOP\aopalliance.jar springAOP\aspectjrt.jar springAOP\aspectjweaver.jar springAOP\spri ...

  4. NSCharacterSet 去除NSString中的空格

    转自:http://blog.sina.com.cn/s/blog_5421851501014xif.html 去除 username中的空格,table newline,nextline 代码如下: ...

  5. JDK的动态代理机制

    JDK Proxy OverView jdk的动态代理是基于接口的,必须实现了某一个或多个随意接口才干够被代理,并且仅仅有这些接口中的方法会被代理.看了一下jdk带的动态代理api.发现没有样例实在是 ...

  6. C语言第十一回合:预处理命令的集中营

    C语言第十一回合:预处理命令的集中营   [学习目标]   1.         宏定义 2.         文件包括"处理 3.         条件编译 预处理命令:能够改进程序设计的 ...

  7. HTML5 多图上传

    HTML5 多图上传 时间 2014-06-05 16:06:29  月小升博客 原文  http://java-er.com/blog/html5-many-image-upload/ 主题 HTM ...

  8. 跟着百度学PHP[14]-PDO之Mysql的事务处理2

    前面所将仅仅是在纯mysql下的讲解,这节就是要将其搬到PDO台面上来了. 将自动提交关闭. SetAttribute下有一个PDO::ATTR_AUTOCOMMIT 将其设置为0即可关闭,如:$pd ...

  9. C#数组、ArrayList和List<T>

    1.数组: 数组在内存中是连续的,索引速度快.赋值与修改简单. 数组的两个数据中间插入数据麻烦,且在声明数组的时候必须指定数组长度.数组长度过长,会浪费内存,过短会造成数据溢出. 2.ArrayLis ...

  10. 解读MT7620A上的DTS文件

    DTS文件,即Device Tree Source,是某些芯片(在Openwrt的target/linux/中,至少ramips,lantiq和BRCM有此文件)用于描述硬件设备资源的文件.此文件是驱 ...