order of arrival of message on a mailboxprocessor
-
28-05-2021 - |
문제
Is there /How can I get guarantee on the order of arrival for the messages sent to a mailboxprocessor
That is, on a thread if I do
agent.post(msg1)
agent.post(msg2)
How can I be sure that, in the treatment loop for the agent, the messages will be received in order ?
해결책
They are. The implementation of Post
is as you might guess, it just adds an item to the queue (on the current thread, under a lock), and posts work to notify any waiting agent to wake up and process it. So if you call Post
twice on the same thread, one after another, the messages get into the queue in that order.
다른 팁
You can also use inbox.Scan(function _ -> None // return an Option)
to find the messages if you have some way of detecting order. Of course, this comes at a price to performance, so leaving the queue alone is the best idea.