boost::interprocess::message_queue 优先级方案

问题描述 投票:0回答:1

操作系统:vxWorks 7 22.09
Boost 库版本:1.75
保护类型:RTP
我正在努力

boost::interprocess::message_queue
在两个 RTP 之间建立 IPC。

文档链接

What's A Message Queue?
A message queue is similar to a list of messages. Threads can put messages in the queue and they can also remove messages from the queue. Each message can have also a priority so that higher priority messages are read before lower priority messages. Each message has some attributes:

A priority.
The length of the message.
The data (if length is bigger than 0).

我打算使用 try_send API。

bool try_send(const void * buffer, size_type buffer_size, 
              unsigned int priority);

我想知道用什么方案来表示较高优先级和较低优先级。

0 - MINIMUM(unsigned int) 表示具有最高优先级,或者 4294967295 - MAXIMUM(unsigned int) 表示具有最高优先级。

POSIX 标准指定了优先级编号方案,其中较高的优先级由较大的数字表示。 VxWorks 本机编号方案与此相反,较小的数字表示较高的优先级。例如,在VxWorks本机优先级编号方案中,最高优先级任务的优先级为0。

boost vxworks
1个回答
0
投票

docs可以按字面意思理解:

什么是消息队列?

消息队列类似于消息列表。线程可以放 队列中的消息,他们还可以从队列中删除消息 队列。 每条消息还可以有一个优先级,以便更高的优先级 优先级消息先于较低优先级消息读取。每个 消息有一些属性:

  • 优先。
  • 消息的长度。
  • 数据(如果长度大于0)。

突出显示的句子包含您的答案:优先级较高的消息先于优先级较低的消息阅读

在实践中,这很容易测试,至少在我的 Linux 机器上:

#include <boost/interprocess/ipc/message_queue.hpp>
#include <iostream>
namespace bip = boost::interprocess;

int main(int argc, char**) try {
    if (argc == 1) {
        // Erase previous message queue
        bip::message_queue::remove("message_queue");

        // Create a message_queue.
        bip::message_queue mq(bip::create_only, // only create
                              "message_queue",  // name
                              100,              // max message number
                              sizeof(int)       // max message size
        );

        // Send 100 numbers
        for (int i = 0; i < 100; ++i) {
            mq.send(&i, sizeof(i), i % 3);
        }
        int i = 999;
        mq.send(&i, sizeof(i), 0);
    } else {
        // Open a message queue.
        bip::message_queue mq(bip::open_only, // only open
                              "message_queue" // name
        );

        unsigned int                  priority;
        bip::message_queue::size_type recvd_size;

        // Receive 100 numbers
        for (int i = 0; i < 100; ++i) {
            int number;
            mq.receive(&number, sizeof(number), recvd_size, priority);
            std::cout << "Received: " << number << " with prio:" << priority << "\n";

            if (i == 999)
                break;
        }
    }
} catch (bip::interprocess_exception const& ex) {
    std::cout << ex.what() << std::endl;
    return 1;
}

打印预期内容:

Received: 2 with prio:2
Received: 5 with prio:2
Received: 8 with prio:2
Received: 11 with prio:2
...
Received: 1 with prio:1
Received: 4 with prio:1
Received: 7 with prio:1
...
Received: 0 with prio:0
...
Received: 96 with prio:0
Received: 99 with prio:0

© www.soinside.com 2019 - 2024. All rights reserved.