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 queuemq_getattr()
— get the current attributes of a message queuemq_notify()
— notify the calling process when the queue becomes nonemptymq_open()
— open or create a message queuemq_receive()
— receive a message from a queuemq_send()
— put a message into a message queuemq_setattr()
— set the flags for a message queuemq_unlink()
— unlink (i.e. delete) a message queue
Is there a way to peek at a message without removing it?
Advertisement
Answer
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.