boost::interprocess::managed_shared_memory(2)(std::deque)
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)的更多相关文章
- boost::interprocess::managed_shared_memory(2)(std::string)
#include <iostream> #include <boost/interprocess/managed_shared_memory.hpp> #include < ...
- boost::interprocess(1)
发送端:#include <iostream> #include <windows.h> #include <string> using namespace std ...
- boost::interprocess::shared_memory_object(1)(基本类型)
#include <iostream> #include <boost/interprocess/managed_shared_memory.hpp> struct pos2d ...
- boost信号量 boost::interprocess::interprocess_semaphore的用法
使用方法首先给信号量初始化赋值,可以根据需要设定需要的值,之前在写项目的过程中用这个控制下载的线程个数. boost::interprocess::interprocess_semaphore m_s ...
- boost::interprocess(2)
//doc_anonymous_mutex_shared_data.hpp #include <boost/interprocess/sync/interprocess_mutex.hpp> ...
- C++ std::deque
std::deque template < class T, class Alloc = allocator > class deque; Double ended queue deque ...
- std::deque
deque容器为一个给定类型的元素进行线性处理,像向量一样,它能够快速地随机访问任一个元素,并且能够高效地插入和删除容器的尾部元素.但它又与vector不同,deque支持高效插入和删除容器的头部元素 ...
- C++ std::deque 基本用法
#include <iostream> #include <string> #include <deque> // https://zh.cppreference. ...
- Boost.Interprocess
https://github.com/svebert/InterprocessMsg 好像消息队列
随机推荐
- B2C电子商务系统研发——产品媒体常见功能点
产品媒体常见功能点 电商研发系列——产品媒体常见功能点 支持图片.视频和文档等媒体类型 产品图片对清晰度要求比极高,但又不能太大,所以图片一般是jpg格式. 视频一般是flv流媒体格式,如果是嵌入产品 ...
- 随机验证码模块(random)
1. 生成一个6位数字+字母的验证码 (面试题) import random l = []for i in range(6): alpha = chr(random.randint(65,90)) a ...
- unity3d一个物体围绕另一个物体旋转
可以使用RotateAround,代码如下: transform.RotateAround (Sun.transform.position, Vector3.down, ); 其中第一个参数是要围绕哪 ...
- 通过使用JanusGraph索引提高性能
翻译整理:纪玉奇 Extending JanusGraph Server JanusGraph支持两种类型的索引:graph index和vertex-centric index.graph inde ...
- 点滴积累【JS】---JS小功能(JS实现侧悬浮浮动)
效果: 思路: 首先,加载onscroll控制滚动条.然后写缓存运动的方法,缓冲运动的方法是先计算出DIV缓冲的速度,并且将其取整,再进行运动判断什么时候到达终点.最后将其参数返回.再在onscrol ...
- 点滴积累【JS】---JS小功能(offsetLeft实现图片滚动效果)
效果: 代码: <head runat="server"> <title></title> <style type="text/ ...
- TabLayout+Fragment+ViewPager+FragmentStatePagerAdapter实现Tab标签
首先来看下实现的效果吧: 最近在项目中实现这个效果的时候.尽管自己磕磕绊绊的实现了,可是知识确实模模糊糊的,今天天气异常的冷,在加上这个知识不太熟练,实在是没有心情进行接下来的计划,干脆借着这个时间, ...
- PCIe to AXI Translation——PCIe 内存空间到AXI内存空间的转换
PCIe to AXI Translation——PCIe 内存空间到AXI内存空间的转换 UltraScale系列芯片包含PCIe的Gen3 Integrated Block IP核在内的多种不同功 ...
- JS高程3:事件
事件是JS和HTML交互的方式. 事件流 事件流是HTML文档接收事件的顺序.分为2个流派:事件冒泡流和事件捕捉流. 事件冒泡流 由内到外 事件捕捉流 由外到内 DOM事件流 事件处理程序 跨浏览器时 ...
- apue编程之getopt ,getopt_long使用方法以及实例
1.getopt 1.1 函数定义 int getopt(int argc, char * const argv[], const char *optstring);#include <unis ...