Volatile versus atomic access
Quick answer
Volatile operations express observable memory access, often for device registers. Atomic operations support synchronized shared-memory access. They solve different problems.
Separate device access from synchronization
Volatile does not make an operation atomic or establish inter-thread happens-before ordering. Device access must obey the hardware manual, access width, alignment and side effects. An atomic operation is only available when the target supports it; do not assume an atomic CPU operation is appropriate for MMIO. Use a verified HAL for actual registers.
fn main() { let value = 7u32; // SAFETY: reference points to initialized, aligned u32; no concurrent writer. let read = unsafe { core::ptr::read_volatile(&value) }; println!("{read}");}Expected stdout (not executed):
7This demonstrates the API, not an actual peripheral register.
use std::sync::atomic::{AtomicU32, Ordering};fn main() { let events = AtomicU32::new(0); events.fetch_add(1, Ordering::Relaxed); events.fetch_add(1, Ordering::Relaxed); println!("{}", events.load(Ordering::Relaxed));}Expected stdout (not executed):
2Relaxed is sufficient for the count alone, not publication of other memory.
Common mistake
Never use read_volatile and write_volatile as a replacement for a lock.
Try it yourself
Explain why a device register with read-to-clear behavior cannot be treated like an ordinary variable.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Volatile versus atomic access — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
Two threads increment an ordinary counter through volatile accesses. Does this prevent data races?
Volatile versus atomic access — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn main() { let value = 7u32; // SAFETY: reference points to initialized, aligned u32; no concurrent writer. let read = unsafe { core::ptr::read_volatile(&value) }; println!("{read}"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Volatile versus atomic access — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::sync::atomic::{AtomicU32, Ordering}; fn main() { let events = AtomicU32::new(0); events.fetch_add(1, Ordering::Relaxed); events.fetch_add(1, Ordering::Relaxed); println!("{}", events.load(Ordering::Relaxed)); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.