mq_timedreceive( ) won't block

I ran into following problem with message queues:

I create message queue using mq_open( ). Then I wan my application
(server) to block until something shows up in a queue. So I use
mq_timedreceive( ) with time out of about one second (at the time my
server is only one process connected to message queue, since other
clients are not written yet :slight_smile:).

I expect to mq_timedreceive( ) to wait, but it won’t. Instead it returns
immediately with ETIMEDOUT, which is annacceptable (causes thread to
poll system very frequently). I worked for about two days over the
problem and I was unable to get correct behaviour. Maybe I missed
something but documentation is incomplete (on the sbject of message
queues) and I was unable to find any code somples, additional docs etc.

Moreover, documentation says that mq_timedreceive( ) should check it’s
arguments, for example buffer size smaller than queue’s message size
should cause mq_timedreceive( ) to return EMSGSIZE. It isn’t so.
Only one argument checked is file descriptor, when I pass bad one, I get
proper error.

Anybody knows what’s wrong ? Am I missing some additional libraries when
linking or maybe something is wrong with mqueue process (but queue gets
created on file system, I can see it with ‘ls’).

I use QNX RTP with patch B installed, calls are with following

for queue creation

(O_EXCL | O_CREAT | O_RDWR ) as flags, NULL for attributes, 0x777 for
permission bits.

and for mq_timedreceive

struct timespec tm;
t.tv_sec = 1;
t.tv_nsec = 0;

if( 0 > mq_timedreceive( fd, buf, 4096, NULL, t ) ) {

Any help apreciated.


PS. Maybe I coul mimic mq_timedreceive( ) using mq_receive( ),
setitimer( ) and SIGALRM handler, but my application is multi - threaded
one, and I have trouble delivering SIGALRM to thread calling
mq_receive( ).
Anyway, this is UN*X, not m$ stuff, and I got used that it’s my fault
when something is wrong here.
So please help me not to change my mind :slight_smile:

I guess someone a little time ago had same problem.
mq_timedreceive parameter for timeout is the absolute time it should stop
to receive. So, you have to add absolute time to the timeout you need.

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

Ricardo K. Ashikawa