How to Use Atomic Types in Rust

Use std::sync::atomic types like AtomicU32 with fetch_add and load methods to safely share mutable state between threads.

Use std::sync::atomic types like AtomicU32 to safely share mutable state between threads without locks. These types provide atomic operations that guarantee thread-safe reads and writes.

use std::sync::atomic::{AtomicU32, Ordering};

let counter = AtomicU32::new(0);

// Atomically increment the value
counter.fetch_add(1, Ordering::Relaxed);

// Read the current value
let current = counter.load(Ordering::Relaxed);