Created
February 6, 2026 18:28
-
-
Save Narrator/b629ab0844225a631e4fd2d7825cef6b to your computer and use it in GitHub Desktop.
Coordination in rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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