Shared via Rust Playground
✓ Published0🌍 Public
FFranklin Chen
Last edited Nov 15, 2015
Created on Nov 15, 2015
This example demonstrates how multiple threads can safely share and mutate a counter using Rust’s concurrency primitives. It shows the `Arc<Mutex<Toaster>>` pattern, where an atomic mutex lock protects the `count` field from race conditions. The code spawns three threads via `thread::spawn`, each cloning the `Arc` handle and acquiring the lock with `toaster.lock().unwrap()` before incrementing the count. The output prints each thread’s index, which is assigned from the prior value of `count` before incrementing. The example uses the standard library’s `std::sync::{Arc, Mutex}` and `std::thread` APIs, with no external data source or rendering—purely console output.
AI-generated description