简体   繁体   English

使用boost :: interprocess :: message_queue多个应用程序安全吗?

[英]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.

相关问题 boost interprocess message_queue和fork - boost interprocess message_queue and fork boost :: interprocess :: message_queue权限被拒绝 - boost::interprocess::message_queue permission denied boost::interprocess::message_queue 在第二个进程中没有收到消息 - boost::interprocess::message_queue no message received in second process boost :: interprocess message_queue-Windows 7低完整性进程 - boost::interprocess message_queue - Windows 7 low integrity process 错误:“ size_type”不是“ boost :: interprocess :: message_queue”的成员 - error: ‘size_type’ is not a member of ‘boost::interprocess::message_queue’ boost::interprocess message_queue 性能 - 相当慢? - boost::interprocess message_queue performance - rather slow? boost :: interprocess :: message_queue使用Visual C ++停止在发布模式下工作 - boost::interprocess::message_queue stops working in Release mode with visual C++ Boost进程间message_queue具有非常量get_num_msg()。 为什么? - Boost interprocess message_queue has non const get_num_msg(). Why? 我可以通过boost :: interprocess :: message_queue发送C ++类对象吗? - Can I send a C++ class object via boost::interprocess::message_queue? 如何知道boost :: interprocess :: message_queue已从系统中删除? - How to get know that boost::interprocess::message_queue was removed from system?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM