简体   繁体   English

POSIX中正确的消息队列使用

[英]Proper message queue usage in POSIX

I'm quite bewildered by the use of message queues in realtime OS. 在实时操作系统中使用消息队列让我感到很困惑。 The code that was given seems to have message queues used down to the bone: even passing variables to another class object is done through MQ. 给出的代码似乎将消息队列用于骨骼:即使将变量传递给另一个类对象也是通过MQ完成的。 I always have a concept of MQ used in IPC. 我总是在IPC中使用MQ的概念。 Question is: what is a proper use of a message queue? 问题是:什么是正确使用消息队列?

In realtime OS environments you often face the problem that you have to guarantee execution of code at a fixed schedule. 在实时操作系统环境中,您经常遇到必须保证以固定时间表执行代码的问题。 Eg you may have a function that gets called exactly each 10 milliseconds. 例如,你可能有一个是被称为究竟每10毫秒功能。 Not earlier, not later. 不早,不迟。

To guarantee such hard timing constraints you have to write code that must not block the time critical code under any circumstances. 为了保证这种硬时序约束,您必须编写在任何情况下都不能阻止时间关键代码的代码。

The posix thread synchronization primitives from cannot be used here. 此处不能使用posix线程同步原语。 You must never lock a mutex or aqurie a semaphore from time critical code because a different process/thread may already have it locked. 您永远不能从时间关键代码中锁定互斥锁或aqurie信号量,因为不同的进程/线程可能已将其锁定。 However, often you are allowed to unblock some other thread from time critical code (eg releasing a semaphore is okay). 但是,通常允许您从时间关键代码中解除阻塞其他一些线程(例如,释放信号量是可以的)。

In such environments message queues are a nice choice to exchange data because they offer a clean way to pass data from one thread to another without ever blocking. 在这样的环境中,消息队列是交换数据的不错选择,因为它们提供了一种干净的方式将数据从一个线程传递到另一个线程而不会阻塞。

Using queues to just set variables may sound like overkill, but it is very good software design. 使用队列来设置变量听起来有点矫枉过正,但这是非常好的软件设计。 If you do it that way you have a well-defined interface to your time critical code. 如果你这样做,你就有了一个明确定义的时间关键代码接口。

Also it helps to write deterministic code because you'll never run into the problem of race-conditions. 编写确定性代码也很有帮助,因为你永远不会遇到竞争条件问题。 If you set variables via message-queues you can be sure that the time critical code sees the messages in the same order as they have been sent. 如果通过消息队列设置变量,则可以确保时间关键代码以与发送时相同的顺序查看消息。 When mixing direct memory access and messages you can't guarantee this. 混合直接内存访问和消息时,您无法保证这一点。

Message Queues are predominantly used as an IPC Mechanism , whenever there needs to be exchange of data between two different processes. 每当需要在两个不同进程之间交换数据时,消息队列主要用作IPC机制 However, sometimes Message Queues are also used for thread context switching. 但是,有时消息队列也用于线程上下文切换。 For eg: 例如:
You register some callback with a software layer which sits on top of driver. 您使用位于驱动程序之上的软件层注册了一些回调。 The callback is returned to you in the context of the driver. 回调将在驱动程序的上下文中返回给您。 It is a thread spawned by the driver. 它是由驱动程序生成的线程。 Now you cannot hog this thread of driver by doing a lot of processing in it. 现在你不能通过在其中进行大量处理来占用这个驱动程序的线程。 So one may add the data returned in callback in a message Queue, which has application threads blocked on it for performing the processing on the data. 因此,可以在消息队列中添加回调中返回的数据,队列中的应用程序线程被阻塞以执行对数据的处理。

I dont see why one should use Message Queues for replacing just normal function calls. 我不明白为什么人们应该使用消息队列来替换正常的函数调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM