boost::进程间消息队列引发错误

boost::interprocess message queue throw error

本文关键字:错误 队列 消息 进程 boost      更新时间:2023-10-16

我在windows中使用boost进程间消息队列,但我遇到了一个问题,当max_msg_size不等于buffer_size时,它会抛出错误,我的部分代码如下:

//process A
message_queue::remove(name);
m_MQ = std::make_unique<message_queue>(create_only, name,2000,300);
m_MQ->try_send(buffer, buffer_size, 0);
//process B
m_MQ = std::make_unique<message_queue>(open_only, name);
m_MQ->try_receive(buffer, buffer_size, recvd_size, priority);

在这种情况下,如果buffer_size不等于300,它将抛出boost::interprocess_exception::library_error,并且我不能再通过可变长度缓冲区。非常感谢。

当您声明。。。

如果buffer_size不等于300,它将抛出boost::interprocess_exception::library_error

您的意思真的是"不等于"吗?还是只有当缓冲区大小小于所述最大消息大小时才会发生异常?

如果只有在buffer_size < 300的情况下才会发生异常,那么我认为这是意料之中的事——库无法可靠地将多达300个char秒的消息接收到大小小于300个char秒的缓冲区中。

相反,您应该使用boost::interprocess::message_queue::get_max_msg_size来创建一个适当大小的接收缓冲区。。。

m_MQ = std::make_unique<message_queue>(open_only, name);
std::vector<char> buffer(m_MQ->get_max_msg_size());
m_MQ->try_receive(buffer.data(), buffer.size(), recvd_size, priority);