通过 boost::interprocess::message_queue 传递指针

发布于 2024-10-10 16:16:52 字数 1008 浏览 0 评论 0原文

我想做的是让应用程序 A 向应用程序 B 发送一个指向 A 已在共享内存上分配的对象的指针(使用 boost::interprocess )。对于该指针传输,我打算使用boost::interprocess::message_queue。显然,来自 A 的直接原始指针在 B 中无效,因此我尝试传输在共享内存上分配的 offset_ptr 。然而,这似乎也不起作用。

进程 A 执行此操作:

typedef offset_ptr<MyVector> MyVectorPtr;
MyVectorPtr * myvector;    
myvector = segment->construct<MyVectorPtr>( boost::interprocess::anonymous_instance )();

*myvector = segment->construct<MyVector>( boost::interprocess::anonymous_instance )
        (*alloc_inst_vec); ;

// myvector gets filled with data here

//Send on the message queue
mq->send(myvector, sizeof(MyVectorPtr), 0);

进程 B 执行此操作:

// Create a "buffer" on this side of the queue
MyVectorPtr * myvector; 
myvector = segment->construct<MyVectorPtr>( boost::interprocess::anonymous_instance )();
mq->receive( myvector, sizeof(MyVectorPtr), recvd_size, priority);

正如我所见,以这种方式,a 执行了偏移指针的位复制,这使他在进程 B 中无效。我该如何正确执行此操作?

What I am trying to do is have application A send application B a pointer to an object which A has allocated on shared memory ( using boost::interprocess ). For that pointer transfer I intend to use boost::interprocess::message_queue. Obviously a direct raw pointer from A is not valid in B so I try to transfer an offset_ptr allocated on the shared memory. However that also does not seem to work.

Process A does this:

typedef offset_ptr<MyVector> MyVectorPtr;
MyVectorPtr * myvector;    
myvector = segment->construct<MyVectorPtr>( boost::interprocess::anonymous_instance )();

*myvector = segment->construct<MyVector>( boost::interprocess::anonymous_instance )
        (*alloc_inst_vec); ;

// myvector gets filled with data here

//Send on the message queue
mq->send(myvector, sizeof(MyVectorPtr), 0);

Process B does this:

// Create a "buffer" on this side of the queue
MyVectorPtr * myvector; 
myvector = segment->construct<MyVectorPtr>( boost::interprocess::anonymous_instance )();
mq->receive( myvector, sizeof(MyVectorPtr), recvd_size, priority);

As I see it, in this way a do a bit copy of the offset pointer which invalidates him in process B. How do I do this right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温馨耳语 2024-10-17 16:16:52

看来您可以按照这篇文章中的描述来解决它增加邮件列表。

It seems you can address it as described in this post on the boost mailing list.

挽你眉间 2024-10-17 16:16:52

我同意这里有一些尴尬,并且 offset_ptr 并不真正适合您想要做的事情。如果指针本身存储在另一个类/结构中(该类/结构也在共享内存段中分配),则 offset_ptr 很有用,但通常您有一些顶级项,它不是某个分配的对象的成员在共享内存中。

您会注意到 offset_ptr example 有点掩盖了这一点 - 它只有一条注释“将列表传达给其他进程”,没有任何细节。在某些情况下,您可能有一个命名的顶级对象,并且该名称可以是您对其进行通信的方式,但如果您有任意数量的顶级对象进行通信,则似乎只是发送来自共享内存基址的偏移量地址是你能做的最好的事情。

您计算发送端的偏移量,发送它,然后添加到接收端的基地址。如果您也希望能够发送 nullptr,您可以像 offset_ptr 那样做,并同意 1 是一个不太可能被使用的偏移量,或者选择另一个不太可能的哨兵值。

I agree there is some awkwardness here and offset_ptr doesn't really work for what you are trying to do. offset_ptr is useful if the pointer itself is stored inside of another class/struct which also is allocated in your shared memory segment, but generally you have some top-level item which is not a member of some object allocated in shared memory.

You'll notice the offset_ptr example kindof glosses over this - it just has a comment "Communicate list to other processes" with no details. In some cases you may have a single named top-level object and that name can be how you communicate it, but if you have an arbitrary number of top-level objects to communicate, it seems like just sending the offset from the shared memory's base address is the best you can do.

You calculate the offset on the sending in, send it, and then add to the base adddress on the receiving end. If you want to be able to send nullptr as well, you could do like offset_ptr does and agree that 1 is an offset that is sufficiently unlikely to be used, or pick another unlikely sentinel value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文