0

I am using boost interprocess message queue in windows, however I met a issue that it will throw an error when max_msg_size doesn't equals to buffer_size,part of my code is as below:

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

In this case, if buffer_size doesn't equal to 300, it will throw boost::interprocess_exception::library_error and I cannot pass variable length buffer any more. Thanks so much.

Gordon
  • 354
  • 1
  • 10
  • 1
    https://stackoverflow.com/questions/52079547/boost-interprocess-message-queue-and-fork/52088610#52088610 – sehe Nov 05 '18 at 14:41

1 Answers1

2

When you state...

if buffer_size doesn't equal to 300, it will throw boost::interprocess_exception::library_error

Do you really mean "not equal to" or does the exception occur only when the buffer size is less than the stated maximum message size?

If the exception only occurs if buffer_size < 300 then I think that's to be expected -- the library can't reliably receive a message that may be up to 300 chars into a buffer that's smaller than 300 chars in size.

Instead you should make use of boost::interprocess::message_queue::get_max_msg_size to create a suitably size receiving buffer...

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);
G.M.
  • 9,830
  • 2
  • 10
  • 17