大家好,
我目前正在尝试寻找一种在 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 错误报告列表中找到的(#6147: message_queue sample fails to compile in 32-bit),此代码必须在 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;
}
}
}
}