expression
Bounded CAS retries with explicit failure
A retry budget bounds the number of attempts, not wall-clock latency. If it expires, the operation must report failure rather than pretend it succeeded. This example uses a strong CAS for a deterministic stale observation; a weak CAS can also fail spuriously. Expected stdout (not executed): false 2 Common mistake: Equating bounded failure with wait-free successful completion of the original operation.
Formal Syntax & Structure
rust syntaxrust
for _ in 0..budget { compare_exchange(...) }Working Example
Example Snippetrust
fn main() { use std::sync::atomic::{AtomicUsize, Ordering::Relaxed}; fn try_claim(a: &AtomicUsize, expected: usize, budget: usize) -> bool { for _ in 0..budget { if a.compare_exchange(expected, 1, Relaxed, Relaxed).is_ok() { return true; } } false } let a = AtomicUsize::new(2); println!("{} {}", try_claim(&a, 0, 3), a.load(Relaxed));}