简体   繁体   English

从消息队列中读取(如果为空则不阻塞)

[英]Reading from message queue (non-blocking if empty)

I'm writing to message queue 我正在写消息队列

if (msgsnd(q, &msg, sizeof(message), slaves_list[to]) == -1)

and reading 和阅读

if (msgrcv(q, &msg, sizeof(message), id, 0) == -1)

but what if this queue is empty? 但如果这个队列是空的怎么办? How to check that? 怎么检查? If there is nothing I want execute next instruction in the loop 如果没有什么我想在循环中执行下一条指令

Use IPC_NOWAIT . 使用IPC_NOWAIT From the documentation : 文档

If (msgflg & IPC_NOWAIT) is non-zero, the calling thread will return immediately with a return value of -1 and errno set to [ENOMSG] . 如果(msgflg & IPC_NOWAIT)非零,则调用线程将立即返回,返回值为-1 ,errno设置为[ENOMSG]

Use IPC_NOWAIT for msgflg parameter: http://man7.org/linux/man-pages/man2/msgsnd.2.html 对msgflg参数使用IPC_NOWAIT: http//man7.org/linux/man-pages/man2/msgsnd.2.html

IPC_NOWAIT Return immediately if no message of the requested type is in the queue. IPC_NOWAIT如果队列中没有所请求类型的消息,则立即返回。 The system call fails with errno set to ENOMSG. 系统调用失败,并将errno设置为ENOMSG。

You can check if queue is empty using the 您可以使用以下命令检查队列是否为空

ipcs

command in linux terminal. linux终端中的命令。

It will show you the queues that you have created. 它将显示您已创建的队列。

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

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