Sharring state: Channels V. shared memory

⚓ Rust    📅 2026-02-03    👤 surdeus    👁️ 6      

surdeus

Friends

I have two threads that need to share some state, thread A holds the state and thread B periodically needs to know the latest state of A

I have to make a choice and I do not have a decision criterion:

  1. I can use shared memory with Arc<Mutex<State>>
  2. I could have a channel mpsc::Sender<State> that A uses to send state to B

I want to conserve resources the best I can, CPU cycles and wall clock time.

Using shared memory carries the overhead of locking and unlocking the Mutex

Using a channel B will have to consume all stale states in a loop to get to the latest

My instincts are to use shared memory and avoid the unknown, but bounded, clearing stale states.

I feel that channels are more "rusty" and I should be using them

I really have no sense of how to balance the two overheads. How should I be thinking about this?

4 posts - 4 participants

Read full topic

🏷️ Rust_feed