简体   繁体   English

处理与boost :: interprocess STL兼容的共享内存分配器创建的C ++对象的正确方法是什么?

[英]What is the correct way to dispose of a C++ object created with boost::interprocess STL compatible shared memory allocators?

I'm a little confused about Boost::interprocess shared memory and deallocation. 对于Boost :: interprocess共享内存和释放,我有些困惑。

When creating a new object on the stack in C++, you simply declare the type, the variable name, and the parameters to the constructor (unless you want a default constructor): 使用C ++在堆栈上创建新对象时,只需向构造函数声明类型,变量名称和参数(除非您需要默认的构造函数):

 AType AVariableName(param1,param2); 

If that same variable is to be allocated using Boost::interprocess I am doing something like this. 如果要使用Boost :: interprocess分配相同的变量,我正在做这样的事情。 I apologize for the dense series of typedefs but I can't think how to ask this question and be specific without giving real boost templated type expansions. 我为密集的typedef系列致歉,但是我不考虑如何提出这个问题,并且在不给出真正的boost模板化类型扩展的情况下具体化。

typedef int                   PointKeyType;
typedef DATAPOINT                 PointMappedType;
typedef std::pair<const int, PointMappedType> PointValueType;
typedef boost::interprocess::allocator<PointValueType, 
  boost::interprocess::managed_shared_memory::segment_manager> PointShmemAllocator;
typedef boost::interprocess::map<PointKeyType, 
  PointMappedType, std::less<PointKeyType>, PointShmemAllocator> PointMap;

So far above all I've done is define that I'm making an <integer,DATAPOINT> std::map-like entity in shared memory and I haven't said what DATAPOINT is, but it's a struct defined with some float, and integer values in it. 到目前为止,最重要的是定义是在共享内存中创建一个<integer,DATAPOINT> std :: map-like实体,我没有说什么DATAPOINT是什么,但这是一个使用浮点数定义的结构,和其中的整数值。 In the application that owns and initial creates the shared memory map I initialize it like this, mostly borrowed code from a Boost demo: 在拥有并初始创建共享内存映射的应用程序中,我像这样初始化它,主要是从Boost演示中借来的代码:

   managed_shared_memory segment
      ( open_or_create  // or maybe it should be create_only?
       ,MEMORY_AREA_NAME // a string literal
       ,65536);          //segment size in bytes


   // STL compatible allocator
   PointShmemAllocator point_alloc_inst (segment.get_segment_manager());

Now I use the allocator like this, creating boost::interprocess::map<...> using the typedef PointMap: 现在,我使用这样的分配器,使用typedef PointMap创建boost :: interprocess :: map <...>:

 PointMap *aPointMap = 
       segment.construct<PointMap>("SOMEOBJECT_NAME_HERE")                                  (std::less<PointKeyType>() //first  ctor parameter
                                 ,point_alloc_inst);     

Now suppose I want to dispose of this boost::interprocess::map object I call a PointMap, and reuse its memory. 现在,假设我要处理此boost::interprocess::map对象,我将其称为PointMap,然后重用其内存。 How do I do that? 我怎么做?

I tried something like this: 我尝试过这样的事情:

segment.destruct<PointMap>(aPointMap);

But the syntax is not exactly orthogonal here. 但是这里的语法不是完全正交的。 Then II thought, maybe it's some thing like the placement syntax of destructors, but I haven't been able to figure it out. 然后,我想,也许就像析构函数的放置语法一样,但是我一直无法弄清楚。

If it's all magic and it just works, and I am just supposed to just delete PointMap , and that's all there is to it, I'll feel a bit silly, but I want to make sure I'm not making a big mistake. 如果这一切都是神奇的并且可以正常工作,而我只想delete PointMap ,仅此而已,我会觉得很傻,但是我想确保自己没有犯大错。

Secondly, I am assuming that the secondary processes that access this shared memory are simply handled the same way, but I would use the find<T> methods like this: 其次,我假设访问该共享内存的辅助进程只是以相同的方式处理,但是我将使用像以下这样的find<T>方法:

      std::pair<PointMap*, std::size_t> f = segment->find<PointMap>("SOMEOBJECT_NAME_HERE");
      aPointMap = f.first;

And then, when I'm done with it, delete aPointMap or just set aPointMap = NULL ? 然后,当我完成操作后, delete aPointMap或只设置aPointMap = NULL

See the docs which give a complete example of what you want to do. 请参阅文档 ,其中提供了您要执行的操作的完整示例。 The map example doesn't destroy the container, but the vectors one does. 地图示例不会破坏容器,但矢量会破坏容器。

PointMap *aPointMap =
       segment.construct<PointMap>("SOMEOBJECT_NAME_HERE")
                                 (std::less<PointKeyType>() //first  ctor parameter
                                 ,point_alloc_inst);

That creates an object named "SOMEOBJECT_NAME_HERE" so to destroy it you just destroy that named object: 这将创建一个名为"SOMEOBJECT_NAME_HERE"的对象,因此只需销毁该命名对象即可销毁它:

segment.destroy<PointMap>("SOMEOBJECT_NAME_HERE");

This is arguably orthogonal: you create an object by name and pass arguments, then destroy it by name (but don't need arguments because destructors don't take arguments.) 这可以说是正交的:您可以按名称创建对象并传递参数,然后按名称销毁它(但不需要参数,因为析构函数不接受参数。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM