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 好像消息队列
随机推荐
- java通过CLASSPATH读取包内文件
读取包内文件,使用的路径一定是相对的classpath路径,比如a,位于包内,此时可以创建读取a的字节流:InputStream in = ReadFile.class.getResourceAsSt ...
- Tcp/Ip--正常情况下的三次握手,四次挥手
三次握手 四次挥手
- Python Kivy 安装问题解决
Fix: Running this was suggested by @matham in #3889 and solves the problem described below:python -m ...
- JavaScript正则表达式基础知识汇总
一.创建正则对象: 1.构造函数RegExp创建正则对象 var pattern = new RegExp('s$'); //pattern匹配以s结尾的字符串 2.使用正则直接量 var patte ...
- php-fig组织fig-standards的一些标准
参考: http://psr.phphub.org/ https://github.com/php-fig/fig-standards https://github.com/PizzaLiu/PHP- ...
- sublimtext3 自定义编译环境
sublime text是一个非常神奇到编辑器,对于我这种小白来说,感觉比vim好用,但是如果用sublime自带到编译环境到话,是没法向程序汇总输入数据的,所以要自己新建编译命令 { "c ...
- 替换jar中的指定文件
替换jar 包下面的class 文件,很多人会想到直接用winrar 打开替换,在一般的情况下, 是可行的,但是如果说这个jar 的代码经过混淆后,会有大小写不同,文件名是相同的,在windos ...
- MySQL5.7的配置文件
5.7 /etc/mysql/mysql.conf.d/mysqld.cnf 5.6 /etc/my.cnf 或 /etc/mysql/my.cnf
- spring的容器(控制反转、依赖注入)
一.spring的容器 ”容器“是spring的一个重要概念,其主要作用是完成创建成员变量,并完成装配. 而容器的特点”控制反转“和”依赖注入“是两个相辅相成的概念. 控制反转:我们在使用一个类型的实 ...
- [转]所有编程皆为 Web 编程
Web编程还远远没有达到完美的境地.其实,还有点乱!没错,随便会写点代码的人就能三下两下地搞出一个糟糕的Web应用:也确实,99%的Web 应用都似狗屎一堆.但是,这也意味着,相当“聪明”的程序员们正 ...