70 %
Chris Biscardi

Rust Atomic values for safe concurrent access

Arc gives us shared references and Atomic types provide primitive shared-memory communication between threads.

Atomic variables are safe to share between threads (they implement Sync) but they do not themselves provide the mechanism for sharing and thus need to be used with Arc.

Atomic types don't internally acquire a global mutex.

use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::Duration;
struct JobStatus {
jobs_completed: AtomicU32,
fn main() {
let status = Arc::new(JobStatus {
jobs_completed: AtomicU32::new(0),
let status_shared = status.clone();
thread::spawn(move || {
for _ in 0..10 {
status_shared.jobs_completed.fetch_add(1, Ordering::Relaxed);
while status.jobs_completed.load(Ordering::Relaxed) < 10 {
println!("waiting... ");

Be Warned: Ordering