making non-shared copies of boost::interprocess shared memory objects(制作 boost::interprocess 共享内存对象的非共享副本)
问题描述
我已经实现了各种旨在用于boost::interprocess
共享内存段的类.他们所有的构造函数都使用 allocator<void,segment_manager>
引用——有些在我写的定义中是明确的(比如下面的 Foo
构造函数),有些只是因为这就是 boost 容器定义要求,在 boost 库代码中,我不应该更改(如下面的 IndexVector
).
I have implemented various classes that are designed to be used in boost::interprocess
shared memory segments. All their constructors employ allocator<void,segment_manager>
references—some explicitly in the definitions I have written (like the Foo
constructor below) and some simply because that's what the boost container definition requires, in boost library code that I should not be changing (like the IndexVector
below).
大多数情况下,这些对象位于共享内存中.但有时我想在非共享内存中创建它们的副本.我的问题是:我是否必须定义一个完全不同的类(例如 Foo_Nonshared
),其中包含不同的成员类型(std::vector
而不是我的共享 >IndexVector
类型)并在它们之间提供复制/转换功能?这将是大量的工作和大量愚蠢的重复.我可以通过为现有的 Foo
类提供替代构造函数来减少重复,但是我不知道如何在没有分配器的情况下初始化 IndexVector
成员.
Mostly, these objects sit in shared memory. But I sometimes want to create copies of them in non-shared memory. My question is this: do I have to define a whole different class (e.g. Foo_Nonshared
) containing different member types (std::vector<Index>
instead of my shared IndexVector
type) and provide copy/conversion functions between them? That will be a lot of work and a lot of stupid duplication. I could reduce duplication by providing an alternative constructor to the existing Foo
class, but then I wouldn't know how to initialize the IndexVector
member without an allocator.
或者有什么好的捷径吗?我正在想象某种特定的 allocator
实例,我可以将它传递给 Foo()
,因此它将传递给 IndexVector
构造函数,这将被两者识别为在非共享内存中分配".这样的事情存在吗?是否有用于管理普通非共享内存的虚拟段管理器"?或者有其他方法可以解决这个问题吗?
Or is there some nice shortcut? I'm imagining some sort of particular allocator
instance that I can pass to Foo()
, and which will hence be passed on to the IndexVector
constructor, which will be recognized by both as meaning "allocate in non-shared memory". Does such a thing exist? Is there a "dummy segment manager" for managing vanilla non-shared memory? Or are there other ways around this problem?
我希望得到与 C++03 兼容的答案,尽管我也有兴趣学习 C++11+ 的做事方式.
I'm hoping for C++03-compatible answers even though I'm also interested to learn the C++11+ ways of doing things.
更新以下问题被标记为重复:我已阅读这些以前的类似问题:
Update following question being marked as duplicate: I have read these previous similar questions:
- boost::interprocess 容器的容器不在共享内存中
- boost::interprocess 容器的容器不在共享内存中复制
并试图概括我在那里看到的内容,有一些成功和一些失败(见下面的列表).有一些编译器错误我无法解决,标记为 ERROR — 特别是我无法弄清楚如何实例化迭代这些高度元"容器的成员的方法.但是无论有没有这些错误,我还无法看到如何将模板的模板变成可维护的解决方案(实际上,我的对象包含其他复杂对象的容器,其中包含更多容器,AFAICS 使语法复杂化,超出理智...见标有嗯"的部分).
and have tried to generalize what I see there, with some successes and some failures (see listing below). There are a few compiler errors that I haven't been able to resolve, marked ERROR—in particular I cannot figure out how to instantiate methods that iterate over the members of these highly "meta" containers. But with or without those errors, I cannot yet see how to make templates-of-templates into a maintainable solution (my objects, in reality, contain containers of other complex objects, which contain further containers, which AFAICS complicates the syntax beyond sanity... see the part marked "hmm").
我想,最后,我可能不得不重新设计以避免在共享内存和堆内存中使用相同的对象.
I guess, in the end, I might have to re-design to avoid having the same objects in shared and heap memory.
推荐答案
好的,您已经遇到了Frequently Annoying Edgecase",即模板-模板参数不是 C++ 中的一等公民(您不能传递它们/typedef 它们):
Ok, you've run into the Frequently Annoying Edgecase that template-template arguments aren't first class citizens in C++ (you cannot pass them around/typedef them):
- 如何传输模板?
我们该怎么办?
allocator::rebind
分配器有一个重新绑定机制,我敢说正是因为这个.所以你可以传递一个 alloc
就好像它是开放模板一样,因为你总是可以通过执行 Alloc::rebind
.
Allocators have a rebind mechanism, I daresay precisely because of this. So you can pass a alloc<void>
as if it is the open template, because you can always get from there to a sibling allocator type by doing Alloc::rebind<T>::other
.
除此之外,分配器通常具有执行此重新绑定的转换构造函数,因此您无需在许多采用分配器的地方过于具体
Add to this the fact that allocators usually have conversion constructors that do this rebinding, you don't need to be overly specific in many places taking allocators
在 c++11 中,引入了 scoped_allocator
以避免必须在许多地方手动传递 allocator
实例,这些地方将进行内部构造元素(例如 emplace_back
).
in c++11, scoped_allocator
s have been introduced to avoid having to manually pass allocator
instances in a number of places that will do internal construction of elements (e.g. emplace_back
).
有一个库魔法,它会自动从容器的 scoped_allocator
添加分配器实例作为最后一个构造函数参数(默认情况下).Boost Container 库已将 scoped_allocator_adaptor
概念向后移植到 c++03,以便您可以使用它.
There's library magic in place that will automatically add the allocator instance from the container's scoped_allocator
as the last constructor argument (by default). Boost Container library has backported the scoped_allocator_adaptor
concept to c++03 so you can use it.
这是一个完整的示例,向您展示了如何解决您遇到的问题,以及如何将基于堆的 Bar
实例与共享内存 Foo
混合使用> 实例:
Here's a full sample that shows you how to solve the issues you had, and also, how you can mix the heap-based Bar
instances with the shared-memory Foo
instance:
由于上面提到的 scoped_allocator
而起作用.
Which works due to the scoped_allocator
mentioned above.
生活在 Coliru
打印:
这篇关于制作 boost::interprocess 共享内存对象的非共享副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!