expression

Spin-style ownership flag: one nonblocking attempt

An atomic boolean can represent a claimed flag. Acquire on successful claim and release on unlock are building blocks of synchronization. This sample makes one attempt per call, touches no protected shared data, and deliberately does not implement a general lock, unsafe guard or busy-wait loop. A spinlock is blocking in the progress-property sense if a stalled owner prevents others from acquiring it. Expected stdout (not executed): true false false Common mistake: Treating a bare flag as a panic-safe lock with a validated protected-data lifetime.

Formal Syntax & Structure

rust syntaxrust
compare_exchange(false, true, Acquire, Relaxed); store(false, Release)

Working Example

Example Snippetrust
fn main() {    use std::sync::atomic::{AtomicBool, Ordering::{Acquire, Release, Relaxed}};    let busy = AtomicBool::new(false);    let first = busy.compare_exchange(false, true, Acquire, Relaxed).is_ok();    let second = busy.compare_exchange(false, true, Acquire, Relaxed).is_ok();    assert!(first);    busy.store(false, Release);    println!("{first} {second} {}", busy.load(Relaxed));}
Rust Reference