Updated: April 19, 2023 |
Receive a message from a message queue
#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 mq_timedreceive() function receives the oldest of the highest priority messages in the queue specified by mqdes. The mq_timedreceive_monotonic() function is a QNX Neutrino extension; it's similar to mq_timedreceive(), but it uses CLOCK_MONOTONIC, so the timeout isn't affected by changes to the system time.
If you want to choose the clock against which the timeout is measured, you can instead use mq_clockreceive(), which is similar to mq_timedreceive() and mq_timedreceive_monotonic() and differs only because of its clock parameter (clk). Choosing the clock prevents the timeout from being affected by changes to the system time.
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.
The size of the message removed from the queue, or -1 if an error occurred (no message is removed from the queue, and errno is set).
See the example for mq_open().
mq_timedreceive() is POSIX 1003.1; mq_timedreceive_monotonic() is QNX Neutrino
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |