Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Implementing Async MPMC Channel in Rust

mox692
February 13, 2025

Implementing Async MPMC Channel in Rust

mox692

February 13, 2025
Tweet

More Decks by mox692

Other Decks in Programming

Transcript

  1. MPMC Channel? • Multi Producer Multi Consumer Channel • i.e.

    have multiple senders and multiple receivers 2 Sender Sender Sender Receiver Receiver
  2. Prior Art 4 kind bounded / unbounded sync / async

    std::mpmc mpmc, mpsc, etc both sync crossbeam mpmc, mpsc, etc both sync async_channel mpmc both async kanal mpmc both sync/async Go channel mpmc both async
  3. Bounded 11 HHH III CCC DDD EEE FFF GGG Sender

    Receiver • If receiver is too slow, sender would block Block!
  4. Bounded 12 HHH DDD EEE FFF GGG Sender Sender Sender

    Receiver Receiver • Actually, multiple senders and receivers work concurrently
  5. Block / Unblock 15 • Sync channel ◦ thread api

    (park, unpark) • Async channel ◦ waker api
  6. Block / Unblock 17 Sender Receiver Waker Waker list •

    When block happens, store waker somewhere
  7. Block / Unblock 18 CCC Sender Receiver Waker1 Waker list

    • When block happens, store waker somewhere Pop from list
  8. Depth of Wakerlist … 🚀 19 • For wakerlist, intrusive

    linked list would be much performant ◦ What are intrusive linked lists? ◦ new internal semaphore based on intrusive lists
  9. How to test concurrent code base? 21 • Multi threaded

    • Complex synchronization • Atomic Operations
  10. About Loom internals … 24 Video 🎥: https://youtu.be/-MRCgyiCxWU?si=a4NNwSxVgke6AFwZ Slide 📝:

    https://speakerdeck.com/kimuramotoyuki/bug-free-concurrency-in-rust