I’m using Posix Message Queues on Linux. Basically, I have multiple threads that receive messages from the same queue by calls to mq_timedreceive.
If more than one thread is running at the same time and the queue is not empty, am I guaranteed that a message does not get received more than once (i.e. the message does not get delivered to multiple threads)?
To be sure, I could synchronize the receive with a mutex, but I’d like to avoid this lock if possible. I read all the man pages (man mq_overview(3)) but without finding anything explicit.
Thanks in advance.
The kernel does that locking for you.
Look at the implementation in ipc/mqueue.c:
Each mqueue has a spinlock, which is acquired before checking for new messages.
The last else (pipelined_receive) is where the message is dequeued. This is protected by the info->lock so there’s no way two threads could get the same message.