Shareing state: Channels V. shared memory
⚓ Rust 📅 2026-02-03 👤 surdeus 👁️ 8Friends
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:
- I can use shared memory with
Arc<Mutex<State>> - I could have a channel
mpsc::Sender<State>thatAuses to send state toB
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?
5 posts - 5 participants
🏷️ Rust_feed