mq_timedreceive(), mq_timedreceive_monotonic()

Получить сообщение от очереди сообщений


#include <mqueue.h>
#include <time.h>
ssize_t mq_timedreceive( mqd_t mqdes,
char *msg_ptr,
size_t msg_len,
unsigned int *msg_prio,
const struct timespec *abs_timeout );
ssize_t mq_timedreceive_monotonic( mqd_t mqdes,
char *msg_ptr,
size_t msg_len,
unsigned int *msg_prio,
const struct timespec *abs_timeout );


The descriptor of the message queue you want to receive a message from, returned by mq_open().
A pointer to a buffer where the function can store the message data.
The size of the buffer, in bytes.
NULL, or a pointer to a location where the function can store the priority of the message that it removed from the queue.
A pointer to a struct timespec that specifies the absolute time (not the relative time to the current time) to wait before the function stops trying to receive messages.



The mq_timedreceive() function receives the oldest of the highest priority messages in the queue specified by mqdes. The mq_timedreceive_monotonic() function is a ЗОСРВ «Нейтрино» extension; it's similar to mq_timedreceive(), but it uses CLOCK_MONOTONIC, so the timeout isn't affected by changes to the system time.

Note: Neutrino supports two implementations of message queues: a traditional implementation, and an alternate one that uses asynchronous messages. For more information, see the entries for mq and mqueue.

If you call mq_timedreceive() with a msg_len of anything other than the mq_msgsize of the specified queue, then mq_timedreceive() returns an error, and errno is set to EINVAL.

If there are no messages on the queue specified, and O_NONBLOCK wasn't set (in the oflag argument to mq_open()), then the mq_timedreceive() call blocks. If multiple mq_timedreceive() calls are blocked on a single queue, then they're unblocked in FIFO order as messages arrive.

In the traditional (mqueue) implementation, calling read() with mqdes is analogous to calling mq_timedreceive() with a NULL msg_prio.

Возвращаемое значение:

Размер сообщения, удаленного из очереди. Если возникла ошибка функция возвращает -1, код ошибки записывается в errno.

Коды ошибок:

The O_NONBLOCK flag was set and there are no messages currently on the specified queue.
The mqdes argument doesn't represent a valid queue open for reading.
The operation was interrupted by a signal.
One of the following:
The given msg_len is shorter than the mq_msgsize for the given queue or the given msg_len is too short for the message that would have been received.
The timeout value was exceeded.


Specify an absolute timeout of 1 second:

struct timespec tm;
clock_gettime( CLOCK_REALTIME, &tm );
tm.tv_sec += 1;
if ( 0 > mq_timedreceive( fd, buf, 4096, NULL, &tm ) )


mq_timedreceive() — POSIX 1003.1 MSG; mq_timedreceive_monotonic() — ЗОСРВ «Нейтрино»

Точка остановки потока
Обработчик прерываний
Обработчик сигналов
В потоке

Тематические ссылки:

mq_close(), mq_open(), mq_receive(), mq_send(), mq_timedsend(), struct timespec

mq, mqueue в Справочнике по Утилитам

Предыдущий раздел: Описание API системной библиотеки