简体   繁体   English

用于进程间共享内存的非 Boost STL 分配器?

[英]Non-Boost STL allocator for inter-process shared memory?

Due to policy where I work, I am unable to use a version of Boost newer than 1.33.1 and unable to use a version of GCC newer than 4.1.2.由于我工作的政策,我无法使用高于 1.33.1 的 Boost 版本,也无法使用高于 4.1.2 的 GCC 版本。 Yes, it's garbage, but there is nothing I can do about it.是的,这是垃圾,但我无能为力。 Boost 1.33.1 does not contain the interprocess library. Boost 1.33.1 不包含进程间库。

That said, one of my projects requires placing an std::map (or more likely an std::unordered_map ) in to shared memory.也就是说,我的一个项目需要将std::map (或更可能是std::unordered_map )放入共享内存中。 It is only written/modified ONE TIME when the process loads by a single process (the "server") and read by numerous other processes.当进程由单个进程(“服务器”)加载并由许多其他进程读取时,它仅被写入/修改一次。 I haven't done shared memory IPC before so this is fairly new territory for me.我以前没有做过共享内存 IPC,所以这对我来说是相当新的领域。 I took a look at shmget() but it would appear that I can't continually use the same shared memory key for allocation (as I assume would be needed with STL container allocators).我查看了shmget()但似乎我不能连续使用相同的共享内存键进行分配(因为我认为 STL 容器分配器需要)。

Are there any other NON-BOOST STL allocators that use shared memory?是否有其他使用共享内存的非 BOOST STL 分配器?

EDIT: This has been done before.编辑:之前已经完成。 Dr. Dobbs had an article on how to do this exactly back in 2003 , and I started to use it as a reference. Dobbs 博士早在 2003 年就有一篇关于如何做到这一点的文章,我开始将其用作参考。 However, the code listings are incomplete and links to them redirect to the main site.但是,代码清单不完整,指向它们的链接重定向到主站点。

EDIT EDIT: The only reason I don't just re-write Boost.Interprocess is because of the amount of code involved.编辑 编辑:我不只是重写 Boost.Interprocess 的唯一原因是因为涉及的代码量。 I was just wondering if there was something relatively short and concise specifically for POSIX shared memory that I could re-write from scratch since data transfers between networks are also subject to a multi-day approval process...我只是想知道是否有专门针对 POSIX 共享内存的相对简短和简洁的内容,我可以从头开始重写,因为网络之间的数据传输也需要经过多天的批准过程......

Pointers do not work in shared memory unless you cannot pin down the shared memory at a fixed address (consistent in all processes).指针在共享内存中不起作用,除非您无法将共享内存固定在固定地址(在所有进程中一致)。 As such, you need specific classes that will either be contiguous (no pointer), or have an offset (and not a pointer) into the memory area in which the shared memory is mapped.因此,您需要特定的类,这些类要么是连续的(没有指针),要么在共享内存所映射的内存区域中有一个偏移量(而不是指针)。

We are using shared memory at work in a pretty similar situation: one process computes a set of data, places it in shared memory, and then signal the other processes that they may map the memory into their own address space;我们在工作中使用共享内存的情况非常相似:一个进程计算一组数据,将其放置在共享内存中,然后通知其他进程他们可以将内存映射到自己的地址空间; the memory is never changed afterwards.之后记忆永远不会改变。

The way we go about it is having POD structures (*) (some including char xxx[N]; attributes for string storage).我们采用的方法是使用POD结构 (*)(有些包括char xxx[N];用于字符串存储的属性)。 If you can actually limit your strings, you are golden.如果你真的可以限制你的字符串,你就是金子。 And as far as map goes: it's inefficient for read-only storage => a sorted array performs better (hurray for memory locality).map而言:只读存储效率低下 => 排序数组性能更好(为​​内存局部性欢呼)。 So I would advise going at it so:所以我建议这样做:

struct Key {
    enum { Size = 318 };
    char value[Size];
};

struct Value {
    enum { Size = 412 };
    enum K { Int, Long, String };
    K kind;
    union { int i; long l; char string[Size]; } value;
};

And then simply have an array of std::pair<Key, Value> that you sort ( std::sort ) and over which you use std::lower_bound for searches.然后只需有一个std::pair<Key, Value>数组,您可以对其进行排序( std::sort ),并使用std::lower_bound进行搜索。 You'll need to write a comparison operator for key, obviously:显然,您需要为键编写一个比较运算符:

bool operator<(Key const& left, Key const& right) {
    return memcmp(left.value, right.value, Key::Size) < 0;
}

And I agree that the enum + union trick is less appealing (interface wise) than a boost variant... it's up to you to make the interface better.我同意 enum + union 技巧比 boost 变体更不吸引人(界面明智)……让界面变得更好取决于你。

(*) Actually, a pure POD is not necessary. (*) 实际上,不需要纯 POD。 It's perfectly okay to have private attributes, constructors and copy constructors for example.例如,拥有私有属性、构造函数和复制构造函数是完全可以的。 All that is needed is to avoid indirection (pointers).所需要的只是避免间接(指针)。

Simple workaround.简单的解决方法。 Create your own "libNotBoost v1.0` from Boost 1.51. The Boost library allows this. Since it's no longer Boost, you're fine.从 Boost 1.51 创建你自己的“libNotBoost v1.0`。Boost 库允许这样做。因为它不再是 Boost,所以你没问题。

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

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