【发布时间】:2013-05-30 19:32:53
【问题描述】:
设置消息队列的O_NONBLOCK标志后,使用mq_send()时如何测试阻塞?
是这样的吗
if (errno == EAGAIN)
printf("Blocking occured\n");
【问题讨论】:
标签: posix message-queue
设置消息队列的O_NONBLOCK标志后,使用mq_send()时如何测试阻塞?
是这样的吗
if (errno == EAGAIN)
printf("Blocking occured\n");
【问题讨论】:
标签: posix message-queue
(1) 您使用mq_getattr 调用。
int mq_getattr(mqd_t mqdes, struct mq_attr *attr);
(2) 这将返回结构 mq_attr,如下所示:
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 */
};
(3) 测试是否设置了 O_NONBLOCK 例如
if (mystruct.mq_flags & O_NONBLOCK) //nonblocking
您可能会问其他问题。如果您想知道 mq_send 在设置队列非阻塞后是否有效,那么您的想法是正确的。如果调用不起作用(因为队列已满,并且您会阻止等待它为您的发送提供空间),那么调用将返回 -1 并且 errno 将设置为 EAGAIN。这并不意味着“发生了阻塞”,而是意味着 会发生阻塞但没有发生,因为队列处于非阻塞模式。因此,当希望调用成功时,您必须稍后再次尝试发送。
【讨论】: