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

最新文章

  1. .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1
  2. PHP数组函数总结
  3. duilib的caption上的Edit无法激活
  4. BeanUtils.copyProperties() 用法
  5. 各种UIButton
  6. UML中几种类间关系:继承、实现、依赖、关联、聚合、组合的联系与区别
  7. 一块神奇的树莓派电子板竟让我学会了Linux系统
  8. bzoj4130: [PA2011]Kangaroos
  9. Unity3D Asset stored 已下载的位置
  10. Android开发中在一个Activity中关闭另一个Activity
  11. Python+django开发环境搭建
  12. getopt、getopt_long和getopt_long_only
  13. IOS学习7——cocoapod安装与使用教程
  14. Java学习点滴——Integer缓存
  15. MyBatis3系列__03几个常用的属性配置
  16. [原]Webpack 3 + AngularJS1.* + Bootstrap 4 + Mapbox-gl
  17. JS学习笔记Day5
  18. mongodb 3.2 分片 + 副本集
  19. 关于javascript中defineProperty的学习
  20. 解决Unity协程无法同步返回的问题

热门文章

  1. (四)Oracle学习笔记—— 常见函数
  2. SGA 的自动管理
  3. vue 基础--&gt;进阶 教程(2): 指令、自定义指令、组件
  4. iOS触摸事件哦
  5. crc32 根据字符串获取校验值
  6. jquery中ajax的相关事件汇总
  7. python 2,3版本自动识别导入
  8. CentOS7 安装lua环境(我是在mysql读写分离用的)
  9. php get_called_class()函数与get_class函数的区别
  10. 自定义闹钟 Reminder