views:

58

answers:

2

I want to share a data with multiple processes. My first attempt is to use Point to point message queue with multiple readers since I read that P2P Msg Queue is very fast.

During my test, it seems like multiple readers are reading from the same queue and once a message is fetched by one reader, other readers will not be able to fetch the same message.

What is a better IPC for sharing data to multiple processes? The data is updated frequently (multiple times per second) so I think WM_COPYDATA is not a good choice and will interfere with the "normal" message queue.

My second attempt will probably be a shared memory + mutex + events

+2  A: 

Point-to-point queues will work fine. Yes, when you send, only one receiver will get the message but the sender can query the queue (by calling GetMsgQueueInfo) to see how many listeners (wNumReaders member of the MSGQUEUEINFO) there are and simply repeat the message that number of times.

ctacke
I actually had thought of that but then I was not sure that the messages will be delivered equally (round-robin). And also, if one of the reader thread decided to take a short break from reading and do some lengthy processing on the received message, will the queued messages be mistakenly delivered to other readers and causing some of the readers to receive the same message multiple times? should I make sure that the readers will never take a part-time job and focus only on reading the messages? Thank you
afriza
A: 

Finally, it's perfectly valid for more than one thread or process to open the same queue for read access or for write access. Point-to-point message queues support multiple readers and multiple writers. This practice allows, for example, one writer process to send messages to multiple client processes or multiple writer processes to send messages to a single reader process. There is, however, no way to address a message to a specific reader process. When a process, or a thread, reads the queue, it will read the next available message. There is also no way to broadcast a message to multiple readers.

Programming Windows Embedded CE 6.0 Developer Reference, Fourth Edition, Douglas Boiling, Page 304

Despite the warning, ctacke's ide seems to be fine for my use cases.

Caveat:
My queue readers need to Sleep(10) after they fetch their share of message to allow other readers to go and fetch messages. Without Sleep(), only one reader process is signaled from waiting.

afriza