收录日期:2019/04/20 17:03:14 时间:2010-06-22 20:33:26 标签:posix,message-queue

POSIX provides a way to read a message queue using its mq_receive function. This function also removes it from the queue. I need to find a way to check to see if a message is in the queue without removing it.

From the Linux mq_overview(7) man page:

Polling message queue descriptors


On Linux, a message queue descriptor is actually a file descriptor, and can be monitored using select(2), poll(2), or epoll(7). This is not portable.

See mq_getattr(3). One of the attributes is mq_curmsgs. It's nice to actually get a queue depth, in addition to the boolean indication you'll get from epoll().

From the Linux manpage:

       struct mq_attr {
           long mq_flags;       /* Flags: 0 or O_NONBLOCK */
           long mq_maxmsg;      /* Max. # of messages on queue */
           long mq_msgsize;     /* Max. message size (bytes) */
           long mq_curmsgs;     /* # of messages currently in queue */
       };

You'd want to add a mq_attr data structure when openning a message queue, then get attributes with mq_getattr function

int mq_getattr(mqd_t mqdes, struct mq_attr *attr);

lastly, look at its mq_curmsgs member