两个C ++应用程序在Linux上共享只读内存区域 [英] Two C++ apps sharing a read-only region of memory on Linux

查看:221
本文介绍了两个C ++应用程序在Linux上共享只读内存区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个进程P1和P2。

I have two processes P1 and P2.

我有这个大的只读资源,称为R,我想P1和P2都有权访问。

I have this large read-only resource, called "R" that I want both P1 and P2 to have access to.

R不仅仅是一个平坦的字节组;它是一堆指向对方的C ++对象。

R is not just a "flat" group of bytes; it's a bunch of C++ objects that point to each other.

我更喜欢P1和P2只共享一个R的副本 - 不知何故有P1加载R到一个区域在内存中(在同一地址的P1和P2中),则P1和P2都可以访问R中的对象作为C ++对象(没有竞争条件,因为所有都是只读的)。

I would prefer that P1 and P2 only share one copy of R -- somehow have P1 load R into a region in memory (that's mmaped in P1 and P2 at the same address), then P1 and P2 can both access the objects in R as C++ objects (no race conditions since all is read only).

任何人都熟悉如何做这个/ gotchas?

Anyone familiar how to do this / gotchas?

推荐答案

实际上类似的东西之前问及解决:

Actually something similar has been asked and solved before:

最好的答案可能适用于你:
使用 boost interprocess库。虽然你仍然不能使用具有虚拟函数的对象(讨厌的vtable指针在共享内存问题之外),但是它们有工具允许你使用共享内存中的其他对象的智能指针,以及在共享内存中分配的自定义分配器std :: vector和std :: map对象。

And the best answer will probably work for you: Use boost interprocess library. While you still can't use objects with virtual functions (nasty vtable pointer outside shared memory issue), they do have tools to let you use smart pointers to other objects inside the shared memory, and custom allocators that allocate inside the shared memory for creating std::vector and std::map objects.

这篇关于两个C ++应用程序在Linux上共享只读内存区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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