开发者

Check to see if a message is in a POSIX message queue w/o removing it from the queue?

开发者 https://www.devze.com 2023-01-04 04:41 出处:网络
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 qu

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

0

精彩评论

暂无评论...
验证码 换一张
取 消