用于进程间共享内存的非Boost STL分配器? [英] Non-Boost STL allocator for inter-process shared memory?

查看:188
本文介绍了用于进程间共享内存的非Boost STL分配器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我使用的政策,我无法使用1.31.3之前的Boost版本,也无法使用4.1.2之前的GCC版本.是的,这是垃圾,但是对此我无能为力.Boost 1.33.1不包含进程间库.

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. Yes, it's garbage, but there is nothing I can do about it. Boost 1.33.1 does not contain the interprocess library.

也就是说,我的一个项目需要将 std :: map (或更可能是 std :: unordered_map )放置到共享内存中.当进程由单个进程(服务器")加载并由众多其他进程读取时,只能写入/修改一次.我之前没有做过共享内存IPC,所以对我来说这是一个相当新的领域.我看了 shmget(),但看来我不能继续使用相同的共享内存密钥进行分配(因为我认为STL容器分配器将需要这样做).

That said, one of my projects requires placing an std::map (or more likely an std::unordered_map) in to shared memory. 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. 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).

是否还有其他使用共享内存的 NON-BOOST STL分配器?

Are there any other NON-BOOST STL allocators that use shared memory?

编辑:此已完成.博士.Dobbs早在2003年就有一篇文章介绍了如何准确地做到这一点,我开始将其用作参考.但是,代码清单不完整,指向它们的链接重定向到主站点.

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. However, the code listings are incomplete and links to them redirect to the main site.

编辑我不只是重写Boost.Interprocess的唯一原因是因为涉及的代码量很大.我只是想知道是否存在一些相对简短的,专​​门针对POSIX共享内存的东西,我可以从头开始重写,因为网络之间的数据传输也需要经过多天的批准程序...

EDIT The only reason I don't just re-write Boost.Interprocess is because of the amount of code involved. 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...

推荐答案

指针不能在共享内存中工作,除非您无法将共享内存固定在固定地址(在所有进程中都一致).因此,您需要特定的类,这些类要么是连续的(无指针),要么在映射共享内存的内存区域中具有偏移量(而不是指针).

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.

我们采用的方法是具有 POD 结构(*)(有些包含 char xxx [N]; 属性用于字符串存储).如果您实际上可以限制您的字符串,那么您就是黄金.而且就 map 而言:对于只读存储来说效率很低=>排序后的数组表现更好(对内存局部性有欢呼).所以我建议这样做:

The way we go about it is having POD structures (*) (some including char xxx[N]; attributes for string storage). 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). 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;
};

然后简单地得到一个数组 std :: pair< Key,Value> 进行排序( std :: sort )并在其上使用std :: lower_bound 进行搜索.您显然需要为键编写一个比较运算符:

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. 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;
}

我同意,枚举+联合技巧比boost变量更具吸引力(在界面方面)……要由您来改善界面.

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.

(*)实际上,不需要纯POD.例如,拥有私有属性,构造函数和复制构造函数是完全可以的.所有需要做的就是避免间接访问(指针).

(*) Actually, a pure POD is not necessary. It's perfectly okay to have private attributes, constructors and copy constructors for example. All that is needed is to avoid indirection (pointers).

这篇关于用于进程间共享内存的非Boost STL分配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆