How to peek into a Linux (POSIX) message queue without removing an item?

▼魔方 西西 提交于 2019-11-27 16:01:22

问题


I need to peek into a message queue without removing them. I will go ahead and remove the message queue item only if it complies to certain criteria. How to do this? Following are the APIs I know — but none seems support peeking.

  • mq_close() — close a message queue

  • mq_getattr() — get the current attributes of a message queue

  • mq_notify() — notify the calling process when the queue becomes nonempty

  • mq_open() — open or create a message queue

  • mq_receive() — receive a message from a queue

  • mq_send() — put a message into a message queue

  • mq_setattr() — set the flags for a message queue

  • mq_unlink() — unlink (i.e. delete) a message queue

Is there a way to peek at a message without removing it?


回答1:


Peeking is probably a bad idea for a message queue because, like sehe noted, the danger of race conditions. Just assume you have peeked a message; since you cannot lock the queue, you will be unable to reliably retrieve the same message you have peeked. If you have two processes receiving mutually exclusive messages from the same queue, you should be thinking about separating these messages into two queues, for clarity of design and race condition stability.

Answer made short: A peek is very unlikely because it would need explicit locking semantics to carry it out stably.



来源:https://stackoverflow.com/questions/7401642/how-to-peek-into-a-linux-posix-message-queue-without-removing-an-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!