繁体   English   中英

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

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

美好的一天,

我目前正试图找到一种在64位进程和32位进程之间传递数据的方法。 由于它是一个实时应用程序,并且都在同一台计算机上运行,​​因此我很难使用共享内存(shm)。

虽然我正在寻找一些使用shm的同步机制,但我觉得在boost :: message_queue上。 然而,它不起作用。

我的代码基本上如下:

发件人部分

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);
}

接收器部分

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;
}

如果两个进程是64位或32位,则此代码可以正常工作。 但如果两个过程不相同,则不起作用。

深入了解boost(1.50.0)代码,您将在message_queue_t :: do_receive(boost / interprocess / ipc / message_queue.hpp)中看到以下行:

scoped_lock lock(p_hdr->m_mutex);

出于某种原因,在使用异构进程时,互斥锁似乎被锁定了。 我的猜测是,互斥锁被抵消了,因此它的值被破坏但我不太确定。

我是否想要完成一些根本不受支持的事情?

任何帮助或建议将不胜感激。

我认为这是关于message_queue中用于指向每条消息的offset_ptr的可移植性,包括头部互斥。 自Boost 1.48.0起,应支持32/64位互操作性,如https://svn.boost.org/trac/boost/ticket/5230所述

在票证建议之后,以下定义(到目前为止)对于我在message_queue的leiu中工作正常:

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

在MSVC下的Boost 1.50.0上,这似乎也需要在message_queue.hpp中使用一个小补丁来解决模板歧义:在调用ipcdetail :: get_rounded_size(...)时抛出参数。

我花了整整一个工作日找出解决方案,最后我做到了。 解决方案部分是James提供的,因此我在32位和64位进程上使用了interop_message_queue。

typedef boost::interprocess::message_queue_t< offset_ptr<void, boost::int32_t, boost::uint64_t>> interop_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