简体   繁体   English

64位和32位进程互通boost :: message_queue

[英]64bit and 32bit process intercommunication boost::message_queue

Good day folks, 美好的一天,

I am currently trying to figure a way to pass data between a 64bit process and a 32bit process. 我目前正试图找到一种在64位进程和32位进程之间传递数据的方法。 Since it's a real-time application and both are running on the same computer I tough using shared memory (shm). 由于它是一个实时应用程序,并且都在同一台计算机上运行,​​因此我很难使用共享内存(shm)。

While I was looking for some synchronization mechanism using shm, I felt on boost::message_queue. 虽然我正在寻找一些使用shm的同步机制,但我觉得在boost :: message_queue上。 However it's not working. 然而,它不起作用。

My code is basically the following: 我的代码基本上如下:

Sender part 发件人部分

message_queue::remove("message_queue");
message_queue mq(create_only, "message_queue", 100, sizeof(uint8_t));
for (uint8_t i = 0; i < 100; ++i)
{
    mq.send(&i, sizeof(uint8_t), 0);
}

Receiver part 接收器部分

message_queue mq(open_only, "message_queue");
for (uint8_t i = 0; i < 100; ++i)
{
    uint8_t v;
    size_t rsize;
    unsigned int rpriority;
    mq.receive(&v, sizeof(v), rsize, rpriority);
    std::cout << "v=" << (int) v << ", esize=" << sizeof(uint8_t) << ", rsize=" << rsize << ", rpriority=" << rpriority << std::endl;
}

This code works perfectly if the two process are 64bit or 32bit. 如果两个进程是64位或32位,则此代码可以正常工作。 But doesn't work if the two process aren't the same. 但如果两个过程不相同,则不起作用。

Looking deeper in boost (1.50.0) code you'll see the following line in message_queue_t::do_receive (boost/interprocess/ipc/message_queue.hpp): 深入了解boost(1.50.0)代码,您将在message_queue_t :: do_receive(boost / interprocess / ipc / message_queue.hpp)中看到以下行:

scoped_lock lock(p_hdr->m_mutex);

For some reason, in the mutex seems to be locked when working with heterogeneous processes. 出于某种原因,在使用异构进程时,互斥锁似乎被锁定了。 My wild guess would be that the mutex is offsetted and therefore it's value is corrupted but I'm not quite sure. 我的猜测是,互斥锁被抵消了,因此它的值被破坏但我不太确定。

Am I trying to accomplish something that is simply not supported? 我是否想要完成一些根本不受支持的事情?

Any help or advice will be appreciated. 任何帮助或建议将不胜感激。

I think this is about the portability of offset_ptr used in message_queue to point to each message, including the header mutex. 我认为这是关于message_queue中用于指向每条消息的offset_ptr的可移植性,包括头部互斥。 32-/64-bit interoperability should be supported since Boost 1.48.0, as addressed in https://svn.boost.org/trac/boost/ticket/5230 . 自Boost 1.48.0起,应支持32/64位互操作性,如https://svn.boost.org/trac/boost/ticket/5230所述

Following the ticket suggestion, the following definition has (so far) worked fine for me in leiu of message_queue: 在票证建议之后,以下定义(到目前为止)对于我在message_queue的leiu中工作正常:

typedef message_queue_t< offset_ptr<void, int32_t, uint64_t> > interop_message_queue;

On Boost 1.50.0 under MSVC this also seems to require a small patch in message_queue.hpp to resolve template ambiguity: casting arguments in calls to ipcdetail::get_rounded_size(...). 在MSVC下的Boost 1.50.0上,这似乎也需要在message_queue.hpp中使用一个小补丁来解决模板歧义:在调用ipcdetail :: get_rounded_size(...)时抛出参数。

I've spent my whole workday figure out the solution and finally I made it work. 我花了整整一个工作日找出解决方案,最后我做到了。 The solution is partially what James provided, so I used the interop_message_queue on both the 32-bit and the 64-bit processes. 解决方案部分是James提供的,因此我在32位和64位进程上使用了interop_message_queue。

typedef boost::interprocess::message_queue_t< offset_ptr<void, boost::int32_t, boost::uint64_t>> interop_message_queue;

The problem is that with this modification the code wouldn't compile, so I also had to add the following, which I found on the boost bug report list ( #6147: message_queue sample fails to compile in 32-bit ), this code has to be place before the boost includes of the message_queue: 问题是,通过这个修改,代码将无法编译,所以我还必须添加以下内容,我在boost bug报告列表中找到了( #6147:message_queue示例无法在32位编译 ),此代码包含在boost包括message_queue之前放置:

namespace boost {
  namespace interprocess {
    namespace ipcdetail {
      //Rounds "orig_size" by excess to round_to bytes
      template<class SizeType, class ST2>
      inline SizeType get_rounded_size(SizeType orig_size, ST2 round_to) {
        return ((orig_size-1)/round_to+1)*round_to;
      }
    }
  }
}

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

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