在共享内存C ++中实例化对象 [英] Instantiating objects in shared memory C++

查看:187
本文介绍了在共享内存C ++中实例化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要多个程序来调用公共库中的函数。库函数访问和更新公共全局内存。每个程序的函数调用需要查看这个公共的全局内存。这是一个函数调用需要看到任何先前的函数调用的更新,即使从另一个程序调用。
为了兼容性,我们对共享库公开的函数如何操作有几个设计约束:

We have a need for multiple programs to call functions in a common library. The library functions access and update a common global memory. Each program’s function calls need to see this common global memory. That is one function call needs to see the updates of any prior function call even if called from another program. For compatibility reasons we have several design constraints on how the functions exposed by the shared library must operate:


  • 任何数据项全部声明的标准数据类型和对象)必须对所有调用者可见,而不管运行代码的线程。

  • 在函数中本地声明的任何数据项只在该函数中可见。

  • 任何标准数据类型或任何类的实例出现在本地或全局或两者。

一个解决方案是将库的公共全局内存放在命名的共享内存中。第一个库调用将创建命名的共享内存并初始化它。随后的程序调用将获得共享内存的地址,并将其用作指向全局数据结构的指针。全局声明的对象实例需要在共享内存中动态分配,而本地声明的对象实例可以放置在调用程序线程的堆栈或本地堆中。问题出现,因为全局内存中的初始化对象可以创建并指向分配(新)额外内存的子对象。这些新的分配还需要在共享内存中,并由所有库调用者看到。另一个复杂性是这些对象,其中包含字符串,文件等,也可以在调用程序中使用。当在调用程序中声明时,对象的内存是调用程序的本地内存,不是共享的。所以对象的代码需要处理任何情况。
对我们来说,解决方案将要求我们覆盖全局布局new,regular new和delete操作符。我们发现一个内存管理系统的设计,看起来像它会工作,但我们还没有找到任何实际的实现。如果任何人都知道Nathan Myers的内存管理设计的实现(http://www.cantrip.org/wave12.html?seenIEPage=1),我将不胜感激的链接到它。或者,如果任何人知道另一个共享内存管理器适应动态分配对象,我也想知道它。我检查了Boost库和所有其他来源,我可以找到,但没有什么似乎做我们需要的。我们不喜欢自己写一个。由于性能和鲁棒性很重要,所以使用经过验证的代码是很好的。先感谢任何想法/帮助。

One solution is to put the library’s common global memory in named shared memory. The first library call would create the named shared memory and initialize it. Subsequent program calls would get the address of the shared memory and use it as a pointer to the global data structure. Object instances declared globally would need to be dynamically allocated in shared memory while object instances declared locally could be placed on the stack or in the local heap of the caller thread. Problems arise because initialized objects in the global memory can create and point to sub-objects which allocate (new) additional memory. These new allocations also need to be in the shared memory and seen by all library callers. Another complication is these objects, which contain strings, files, etc., can also be used in the calling program. When declared in the calling program, the object’s memory is local to the calling program, not shared. So the object’s code needs to handle either case. It appears to us that the solution will require that we override the global placement new, regular new and delete operators. We found a design for a memory management system that looks like it will work but we haven’t found any actual implementations. If anyone knows of an implementation of Nathan Myers’ memory management design (http://www.cantrip.org/wave12.html?seenIEPage=1) I would appreciate a link to it. Alternatively if anyone knows of another shared memory manager that accommodates dynamically allocating objects I would love to know about it as well. I've checked the Boost libraries and all the other sources I can find but nothing seems to do what we need. We prefer not to have to write one ourselves. Since performance and robustness are important it would be nice to use proven code. Thanks in advance for any ideas/help.

感谢您对ATL和OSSP库的建议。我现在检查他们,现在虽然恐怕ATL太中心,如果目标是Unix。

Thanks for the suggestions about the ATL and OSSP libraries. I am checking them out now although I'm afraid ATL is too Wincentric if are target turns out to be Unix.

另一件事现在我们似乎很清楚。由于可以在执行期间动态创建对象,所以内存管理方案必须能够分配共享内存的其他页面。现在开始看起来像一个成熟的堆替换内存管理器。

One other thing now seems clear to us. Since objects can be dynamically created during execution, the memory management scheme must be able to allocate additional pages of shared memory. This is now starting to look like a full-blown heap replacement memory manager.

推荐答案

查看 boost.interprocess

这篇关于在共享内存C ++中实例化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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