我在一个进程中的Boost Interprocess managed_shared_memory
中创建了一个map<int, vector<int>>
,然后想在另一个进程中打开它并继续写入。另一个进程试图在对象Communication
的构造函数中打开共享内存,然后给出一个方法addData(int _data)
。
范例:
// Typedefs for shared data structures
using boost::interprocess;
using std::pair;
using std::scoped_allocator_adaptor;
typedef managed_shared_memory::segment_manager man;
typedef allocator <int , man> Int_Allocator;
typedef vector <int, Int_Allocator> Int_Vector;
typedef pair <const int, Int_Vector> Int_Vector_Map_Type;
typedef scoped_allocator_adaptor <allocator<Int_Vector_Map_Type, man>> Int_Vector_Map_Type_Allocator;
typedef map <int, Int_Vector, std::less<int>, Int_Vector_Map_Type_Allocator> Int_Vector_Map;
// Let's call this process 'parent'
managed_shared_memory segment(create_only, "Shared");
Int_Vector_Map_Type_Allocator alloc = segment.get_segment_manager();
segment.construct<Int_Vector_Map>("Map")(std::less<int>(), alloc);
// 'child' process
class Communciation{
Int_Vector_Map* map;
public:
Communication{
managed_shared_memory segment(open_only, "Shared");
map = segment.find<Int_Vector_Map>("Map").first;
}
void addData(int _key, int _value){
if(map->size() == 0 || map->find(_key) == map->end(){
managed_shared_memory segment(open_only, "Shared");
Int_Allocator alloc = segment.get_segment_manager();
map->insert(Int_Vector_Map_Type(_key, Int_Vector(alloc)));
} // create the vector and its allocator, otherwise runtime error
map->at(_key).push_back(_value);
}
}
这是一个分解的例子,但我没有看到即时错误。当试图运行此代码时,我得到运行时错误“未处理的异常:EXCEPTION_ACCESS_VIOLATION阅读地址(...)"。将addData(...)
方法更改为:
void addData(int _key, int _value){
managed_shared_memory segment(open_only, "Shared");
map = segment.find<Int_Vector_Map>("Map").first;
if(map->size() == 0 || map->find(_key) == map->end(){
Int_Allocator alloc = segment.get_segment_manager();
map->insert(Int_Vector_Map_Type(_key, Int_Vector(alloc)));
} // create the vector and its allocator, otherwise runtime error
map->at(_key).push_back(_value);
}
型
也就是说,每次调用都打开map解决了这个问题,但在我的情况下并不适用,因为我希望能够在每帧中多次调用此方法,而不会对fps产生太大影响。
这个问题的原因是什么,是否可以使用所描述的boost的interprocess?
**编辑:**对于添加的上下文,'child'进程正在另一个进程中运行,我在其中注入了一个dll。
1条答案
按热度按时间mo49yndu1#
答案有很多组成部分:
emplace
方法)我有很好的使用Boost Container实现的经验,所以让我们展示一下。
简化类型别名
字符串
现在你可以简单地说:
型
它将扩展到正确的比较器,分配器等集合。
生命周期
要从构造函数构造段,您需要使用初始化器列表。例如:
型
Emplace
要插入新元素,请像这样实现
addData
:型
请注意,段只是存在。请注意,分配器是由于作用域分配器适配器与
emplace
构造的组合而传播的。编辑很遗憾我无法编译emplace。我肯定我错过了一些琐碎的东西,但我已经解决了它,比如:
型
现场演示
Live On Coliru
型
印刷
型
现场演示本地: