提问人:Dark Sorrow 提问时间:7/28/2023 更新时间:7/29/2023 访问量:29
boost::interprocess::message_queue 优先级方案
boost::interprocess::message_queue priority scheme
问:
操作系统: vxWorks 7 22.09
Boost Library 版本: 1.75
保护类型: RTP 我正在努力在两个 RTP
之间建立 IPC。boost::interprocess::message_queue
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。
答:
1赞
sehe
7/29/2023
#1
文档可以按字面意思理解:
什么是消息队列?
消息队列类似于消息列表。线程可以放 队列中的消息,它们还可以从 队列。每条消息还可以有一个优先级,以便更高的 优先级消息在优先级较低的消息之前读取。每 message 具有一些属性:
- 优先事项。
- 消息的长度。
- 数据(如果长度大于 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
评论
0赞
Dark Sorrow
7/29/2023
谢谢。我将在vxWorks上进行相同的测试
评论