Fearless Shared State Concurrency


Is it possible to use shared-state concurrency as described in The Rust Book in order to have multiple threads that updated a shared state iteratively? In the book they have the concurrent threads access the data, but then the main thread aggregates them and determines the final result. Is it possible to have the threads share a state, and when one thread updates the state it’s pushed to all the other threads so that all threads always have the latest version of the state? Thanks :slight_smile:



You’re talking about message passing. The book shows how to do what you want without message passing, using Arc<T> instead: https://doc.rust-lang.org/book/ch16-03-shared-state.html#atomic-reference-counting-with-arct

For message passing style, you are going to want a CRDT.

1 Like


You can have shared state via AtomicUsize. For larger data types you’ll need RwLock/Mutex.

1 Like


Can also use fancier primitives in some cases like https://crates.io/crates/arc-swap by @vorner

1 Like