Skip to content

Instantly share code, notes, and snippets.

@Narrator
Created February 6, 2026 18:28
Show Gist options
  • Select an option

  • Save Narrator/b629ab0844225a631e4fd2d7825cef6b to your computer and use it in GitHub Desktop.

Select an option

Save Narrator/b629ab0844225a631e4fd2d7825cef6b to your computer and use it in GitHub Desktop.
Coordination in rust
// High coordination: Mutex — consistent, slower
let counter = Arc::new(Mutex::new(0));
{
let mut c = counter.lock().unwrap(); // Wait for lock
*c += 1;
}
// Less coordination: Atomic — faster, limited operations
let counter = AtomicU64::new(0);
counter.fetch_add(1, Ordering::Relaxed); // No waiting
// Even less: per-thread counters, merge later
thread_local! { static COUNT: Cell<u64> = Cell::new(0); }
// Fast, but only eventually consistent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment