We are in the process of replacing an internal implementation of a message queue (due to limitations of the overall design), and I'd like to use boost::interprocess::message_queue
as a nearly drop-in replacement.
However, we have a specific requirement that in the case that the message queue is "full" (the consuming application is either disconnected or lagging behind), that the "oldest" messages be discarded.
We can accomplish this easily like this:
do
{
if(sent = message_queue.try_send(...))
{
break;
}
else
{
message_queue.receive(...);
}
}
while(true);
However, I can't find reference in the documents that state this is safe. It's obviously not a traditional use of a message queue (to consume it from multiple applications), but is it guaranteed to work?
It's safe. The relevant part of the doc is:
Threads can put messages in the queue and they can also remove messages from the queue.
You can also look at the implementation, which is entirely in the header boost/interprocess/ipc/message_queue.hpp
, in particular the private member function do_receive()
. If you ignore all the shared memory machinery, it's just a circular buffer (as of Boost 1.52) protected with a mutex.