Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single Process Thread Safety Using POSIX Message Queues

I understand that POSIX Message Queues do not require synchronization between different processes. However, how safe are they when being accessed by multiple threads in a single process?

I read this question: Does message queue support Multi-thread? which pertains to SysV Message Queues, and I would assume that POSIX has at least the same support. It seems to imply yes they are thread safe, however:

"Any resource which is shared globally among threads or processes is subject to race conditions"

This would lead me to believe that intra-process synchronization is still required.

In my case in particular both processes that communicate using the message queue implement the 'boss-worker' pattern, and therefore workers can collide into race conditions when attempting to perform operations on the message queues. Would my assumption be correct in stating that access to these queues still requires synchronization within each process?

like image 511
JNYRanger Avatar asked Mar 29 '15 17:03

JNYRanger


1 Answers

If you're passing memory addresses of shared memory (either between threads in a process, or memory that's shared between processes) as the contents of your messages, then you still need memory synchronization to access that memory once you read its address out of the message. But you do not need any additional synchronization to perform operations on the message queue itself (aside from destroying it, which you should not do until the last user has finished with it, of course). That's the whole point of having message queues.

like image 179
R.. GitHub STOP HELPING ICE Avatar answered Sep 29 '22 06:09

R.. GitHub STOP HELPING ICE