【问题标题】:Transfer a pointer through boost::interprocess::message_queue通过 boost::interprocess::message_queue 传递一个指针
【发布时间】:2011-06-04 04:53:59
【问题描述】:

我想要做的是让应用程序 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);

如我所见,通过这种方式,对偏移量指针进行了一点复制,这使他在进程 B 中无效。我该如何正确地做到这一点?

【问题讨论】:

    标签: boost message-queue interprocess


    【解决方案1】:

    看来您可以按照 boost 邮件列表中 this post 中的说明解决它。

    【讨论】:

      【解决方案2】:

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

      您会注意到offset_ptr example 对此进行了掩饰——它只是有一个注释“与其他进程通信列表”,没有详细信息。在某些情况下,您可能有一个命名的顶级对象,并且该名称可以是您通信它的方式,但如果您有任意数量的顶级对象进行通信,它似乎只是从共享内存的基础发送偏移量地址是你能做的最好的。

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

      【讨论】:

        猜你喜欢
        • 2011-09-06
        • 1970-01-01
        • 2019-04-21
        • 2011-03-28
        • 1970-01-01
        • 1970-01-01
        • 2017-05-21
        • 1970-01-01
        • 2015-03-28
        相关资源
        最近更新 更多