问题
I have multiple processes monitoring an MSMQ queue. I want to do multi-step operations like first peek the message and then based on some criteria receive the message. A single message may pass the receiving criteria of multiple processes so that more than one process may try to receive the same message. Will these operations be thread safe? If not what should I do to prevent one process from failing to receive the message that other process has already received?
回答1:
The operations themselves are thread safe. However, if you perform a multi-step operation you may find that the results are not consistent (for example, peeking to see if data is in the queue, then calling to receive the data, only to discover it's not there any longer).
回答2:
According to MSDN:
Only the following methods are thread safe: BeginPeek, BeginReceive, EndPeek(IAsyncResult), EndReceive(IAsyncResult), GetAllMessages, Peek, and Receive.
回答3:
You may be interested in a blog entry I wrote on this subject.
In short, MSMQ C++ API is thread safe, but not all System.Messaging methods are thread safe. In the blog entry I discuss how to call MessageQueue.Send in a thread-safe way.
来源:https://stackoverflow.com/questions/224618/is-msmq-thread-safe