Use std::sync::atomic types like AtomicUsize to perform lock-free, thread-safe operations on shared data. Wrap your value in an atomic type and call methods like .fetch_add() to modify it safely across threads without explicit mutexes.
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread;
let counter = Arc::new(AtomicUsize::new(0));
let mut handles = vec![];
for _ in 0..10 {
let c = Arc::clone(&counter);
let handle = thread::spawn(move || {
c.fetch_add(1, Ordering::SeqCst);
});
handles.push(handle);
}
for h in handles { h.join().unwrap(); }
println!("Result: {}", counter.load(Ordering::SeqCst));