简体   繁体   English

从Boost InterProcess共享内存中检索共享向量

[英]Retrieving shared vectors from Boost InterProcess Shared Memory

I have created a Boost Shared memory, for the purpose of sharing vectors. 我创建了一个Boost共享内存,用于共享向量。

The sharing has been accomplished. 共享已完成。

However, I do not understand how the vectors are pushed into shared memory. 但是,我不明白如何将向量推入共享内存。

I do a push_back to the shared memory from the writing process. 我在写入过程中对共享内存进行了push_back So the vectors are being pushed like a stack push into the shared memory, in LIFO order? 那么,向量像堆栈一样push LIFO顺序push入共享内存中?

The other application, the reader, retrieves the vector in the following fashion : 另一个应用程序(读取器)以以下方式检索向量:

managed_shared_nmemory segment (open_only, "Shared_mem_name");
Vector = segment.find<VECTOR_TYPE>("Vector_name").first;

if (Vector != NULL)
{
    //CODDE
}

Now here, which vector am I reading. 现在在这里,我正在阅读哪个向量。 the one pushed in last ( newest one )? 最后一个(最新的)? And if I'm reading it, does it imply the vector is popped? 如果我正在阅读它,是否意味着该向量已弹出? ie is it still there in the shared memory after the read, and if so, will the shared memory overflow after a time, and how would I stop it? 即,读取后共享内存中是否仍然存在该共享内存,如果是这样,共享内存会在一段时间后溢出,我如何停止它? I don't see anything in the documentation regarding it... 我在文档中没有看到任何关于它的信息...

The vectors aren't "pushed" into the shared memory. 向量不会“推送”到共享内存中。 There is no implicit stack. 没有隐式堆栈。

Instead, the vector lives in the shared memory. 相反,矢量在共享内存。 What you're reading is in fact the very same vector as in another process, at the very same time . 你正在阅读其实是在非常相同的向量在另一个进程中, 在同样的时间

Note that this strongly implies that you need a shared mutex to synchronize access too, otherwise you'll have data races between the threads (from different processes) and by definition this is Undefined Behaviour in C++. 请注意,这强烈暗示着您也需要共享互斥体来同步访问,否则,线程之间(来自不同进程)将发生数据争用,并且根据定义,这是C ++中的未定义行为


On a slightly different angle: 在稍微不同的角度上:

I do a push_back to the shared memory from the writing process. 我在写入过程中对共享内存进行了push_back So the vectors are being pushed like a stack push into the shared memory, in LIFO order? 那么,向量像堆栈一样按LIFO顺序推入共享内存中?

Breaking it down: 分解:

  • I do a push_back to the shared memory 我对共享内存执行push_back

    No you don't. 不,你不会。 You push a vector element to the back of one vector. push一个向量元素push一个向量的back This vector is the single thing you shared, under the name "Vector_name" 此向量是您共享的单个内容,名称为"Vector_name"

  • So the vectors are being pushed 因此向量被推

    No, your vectors stay where they are: in shared memory. 不,您的向量保持原样:位于共享内存中。

  • like a stack push into the shared memory, in LIFO order? 像堆栈按LIFO顺序推入共享内存?

    No, there's only 1 vector. 不,只有1个向量。 A vector is itself a ordered sequential container. 向量本身就是有序的顺序容器。 It supports random access, so 它支持随机访问,因此

     for (auto it = v.begin(); it!= v.end(); ++it) { // process `*it` in order } 

    vs VS

     for (auto it = v.rbegin(); it!= v.rend(); ++it) { // process `*it` in reversed order } 

    If you always use push_back to insert, then in order iteration is like "FIFO" 如果您始终使用push_back进行插入,则in order迭代就像“ FIFO”

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

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