How to use Arc RwLock for shared mutable state

Share mutable state across threads in Rust by wrapping data in Arc<RwLock<T>> for safe concurrent access.

Use Arc<RwLock<T>> to share mutable state across threads safely by wrapping the data in an RwLock for synchronization and an Arc for reference counting.

use std::sync::{Arc, RwLock};

let data = Arc::new(RwLock::new(0));
let data_clone = Arc::clone(&data);

// Read access (multiple readers allowed)
let val = *data.read().unwrap();

// Write access (exclusive writer)
let mut guard = data_clone.write().unwrap();
*guard += 1;