Atomic Message Queues in Rust?


In Python, there is an atomic message queue module (Queue) mainly used to send "emails" between threads, as explained by Raymond Hettinger in his talk. Using atomic message queues makes code much easier to maintain. Queue is an awesome concurrency tool.

I wonder if there is a Rust equivalent. Googling "rust atomic message queue" comes up with nothing relevant. The mpsc module seems similar but the API looks different. Does Rust include a built-in atomic message queue, or does one has to implement it?

Mpsc is exactly what you need, unless you need multiply consumers. What's wrong with its api?

1 Like

"Atomic" is misleading. In low level sense (that rust standard uses) they are single machine instructions. Where as here it is a single function call which uses multiple machine instructions, likely with locking inside.

1 Like

A useful way to reason about the many meanings of "atomic" is in terms of observable behaviour. If the expected client of a write operation cannot observe it as half-done, then it's atomic:

  • Atomic CPU instructions are not observable as half-finished by other CPU cores
  • Atomic concurrent data structures protected using locking are not observable as half-modified by other threads (which will just block until the lock is ready again)
  • Atomic database transactions are not observable as half-performed by other clients of the database which concurrently issue read requests