共享内存空间可以将数据(非POD)发送到另一个共享内存吗?

时间:2022-08-14 22:46:33

I am working on an open-source project built to support parallel computation on workstation networks, like tuple spaces, distributed shared memory, message passing etc.

我正在开发一个开源项目,用于支持工作站网络上的并行计算,如元组空间,分布式共享内存,消息传递等。

Instead of going too broad, let's assume we have some sort of pseudo-IPC communication that does NOT involve processing multiple types, but it should handle a huge amount of queries in a limited amount of time. My solution to this would be using two shared memory pools or at least two shared sections of memory pools.

我们假设我们有一些不涉及处理多种类型的伪IPC通信,但它应该在有限的时间内处理大量的查询,而不是过于宽泛。我的解决方案是使用两个共享内存池或至少两个内存池的共享部分。

I was thinking of #pragma data_seg or two memory mapped files, but that means all of the processes must use the name or the handle of the same file mapping object and I'm not sure if it's bad for me or not. Two instances of the same class can comprehend the data that is in the shared memory, but what if the instances are being modified during the transition to the second shared memory pool?

我在考虑#pragma data_seg或两个内存映射文件,但这意味着所有进程必须使用同一文件映射对象的名称或句柄,我不确定它是否对我不好。同一个类的两个实例可以理解共享内存中的数据,但是如果在转换到第二个共享内存池期间修改实例会怎么样?

Would the second shared memory space comprehend the processed information from the first one without slowing the freshly created 'ecosystem' of data?

第二个共享内存空间是否会理解来自第一个的已处理信息,而不会减慢新创建的数据生态系统?

I hope I managed to rewrite the answer properly.

我希望我能够正确地重写答案。

1 个解决方案

#1


The question isn't fully formed - POD types will be absolutely fine - however you bring up std::string which means you are looking at non-pod types with pointers and the like.

问题还没有完全形成 - POD类型绝对没问题 - 但是你提出了std :: string,这意味着你正在查看带有指针等的非pod类型。

You have to be careful with pointers. Boost takes an approach of smart pointers which work on the following premise: the offets are safe relative to the start of the shared memory region and can be shared across processes. But to get it to another memory pool, you'll need to properly copy it - by allocating an instance in the second shared memory and copy the instance to that.

你必须小心指针。 Boost采用智能指针的方法,其工作原理如下:相对于共享内存区域的开始,offets是安全的,并且可以跨进程共享。但要将其转移到另一个内存池,您需要正确复制它 - 通过在第二个共享内存中分配实例并将实例复制到该内存池。

#1


The question isn't fully formed - POD types will be absolutely fine - however you bring up std::string which means you are looking at non-pod types with pointers and the like.

问题还没有完全形成 - POD类型绝对没问题 - 但是你提出了std :: string,这意味着你正在查看带有指针等的非pod类型。

You have to be careful with pointers. Boost takes an approach of smart pointers which work on the following premise: the offets are safe relative to the start of the shared memory region and can be shared across processes. But to get it to another memory pool, you'll need to properly copy it - by allocating an instance in the second shared memory and copy the instance to that.

你必须小心指针。 Boost采用智能指针的方法,其工作原理如下:相对于共享内存区域的开始,offets是安全的,并且可以跨进程共享。但要将其转移到另一个内存池,您需要正确复制它 - 通过在第二个共享内存中分配实例并将实例复制到该内存池。