[英]Is it safe to consume a boost::interprocess::message_queue multiple applications?
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. 由于整个设计的局限性,我们正在替换消息队列的内部实现,并且我想使用
boost::interprocess::message_queue
作为几乎替代的替换。
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()
. 您还可以查看实现,它完全在标题
boost/interprocess/ipc/message_queue.hpp
,尤其是私有成员函数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. 如果您忽略所有共享内存机制,那么它只是一个由互斥锁保护的循环缓冲区(从Boost 1.52开始)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.