POSIX pr开发者_如何转开发ovides 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
精彩评论