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

标签 c linux message-queue sysv-ipc

我正在写消息队列

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

阅读

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

但是如果这个队列是空的呢?如何检查?如果没有我想在循环中执行下一条指令

最佳答案

使用IPC_NOWAIT。来自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].

关于c - 从消息队列中读取(如果为空则非阻塞),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13974512/

相关文章:

C 使用n+1维数组作为n维数组参数

c - 我想绑定(bind)到特定端口,但端口仍然是随机的

mysql - 将数组插入 MongoDB

azure - 以队列为中心的工作模式的故障处理

在 Unix 上为 OpenVMS 编译

c++ - 带有插入符号操作的字符 XOR

linux - 如何在 Linux 中从文件运行 Bash 'export' 命令?

Linux RFID 读取器 HID 设备与驱动程序不匹配

azure - 消费者如何处理服务总线主题?

oracle - WebSphere MQ/MQSeries - 是否可以通过一次调用将消息发送到多个队列?