惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
Y
Y Combinator Blog
博客园_首页
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
Hugging Face - Blog
Hugging Face - Blog
宝玉的分享
宝玉的分享
B
Blog
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
WordPress大学
WordPress大学
L
LangChain Blog
爱范儿
爱范儿
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
C
Check Point Blog
博客园 - 聂微东
云风的 BLOG
云风的 BLOG
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Help Net Security

The Rust Programming Language Forum - Latest posts

What's everyone working on this week (21/2026)? Rust Function Call with one Param Iced + Canvas + Text: How do I create a style for canvas text "Value dropped while borrowed" as a lifetime mismatch Weird `use of moved value` behaviour Slightly surprising behavior of a while loop Clap: how to disable options after a certain positional argument How do i tell rust-analyzer what kind of bracket to use for a macro? Iterator + Borrow Checker: Work around borrowing already borrowed mutable variable Requesting data via mavlink SKILLS.md for Rust development 🧵 Stringlet UTF-8 Hack Option Niche? Iterator + Map - How do divide every element, including the last element, by the last element Why can't we store returned value of a function in a variable if one of the parameters goes out of scope My first crate :D, floop: A more convenient and less error prone replacement for loop `{ select! { .. }}` Sorting is slow because architectures are wrong — zan-sort redesigns the architecture, not the algorithm Iced Font - Create a custom monospace font to display dollar amounts Is the UnsafePinned RFC wrong about being able to return `&mut T` from `get_mut_unchecked`? Fixing Polars 0.37 compilation errors: Hashbrown 0.17 dependency conflict and decimal parsing Any plans for improving error diagnostic in Rust 2.0? How do I build completely offline? Is `&mut T -> &mut ManuallyDrop<T>` well-defined and sound? Would you use this? — fixtura, declarative fake data injection for tests Foreign trait restrictions on native types make generics hard to use Cargo Exclude Directive Compiler reasoning around a modulo counter Multiple mutable references to elements within one vector Handling non-`Send` data in a `Send` closure Review: Static Multi Pool Allocator Rmquickjs - High-level MicroQuickJS bindings for Rust
Is RMW operation less prone to reading stale values than ...
xmh0511 · 2026-04-17 · via The Rust Programming Language Forum - Latest posts

April 17, 2026, 9:52am 1

use std::sync::atomic::{AtomicBool, AtomicI32, Ordering};
use std::thread;

fn main() {
    let v = AtomicI32::new(0);
    let flag = AtomicBool::new(false);

    thread::scope(|s| {
        s.spawn(|| {
            while !flag.load(Ordering::Relaxed) { // #1
                std::hint::spin_loop();
            }
            assert_eq!(v.swap(2, Ordering::Relaxed), 1); // #2
            // assert_eq!(v.load(Ordering::Relaxed),1); // #5
        });


        s.spawn(|| {
            if v.swap(1, Ordering::Relaxed) == 0 { // #3
                flag.store(true, Ordering::Relaxed); // #4
            }
        });
    });
}

In this example, the assert at #2 never fails. However, if you comment out #2 and uncomment #5, the assert at #5 can fail because #5 can either read 0 or 1.

Does it mean that an RMW is less prone to reading stale values than a pure load? So, for flag checking, we should use RMW operations rather than a pure load? If not that, what's the plausible reason here?

absolutely not. if you want ordering relations you should use the relevant atomic orderings and/or premade locks.

that being said, it is true that swap is less prone to reading "stale" values from a specific atomic in some sense. indeed the total modification order of v must be preserved, so in this specific case given #3 swapped v from 0 to 1, and #2 swapped v from something to 2, and v started at 0, it must necessarily be that what #2 swapped from 1.

in other words swap guarantees that if you only modify through swap, each value that is being swapped in will only be swapped out once.

you can think of it like if integers were !Copy, and swap was like core::mem::swap

mroth April 17, 2026, 2:59pm 3

You make wrong assumptions:

a.) The execution order of the two spawned threads is arbitrary. The second thread can start, execute and finish before the first thread even started.

b.) There is no stale data. There is only Ordering. By using Relaxed you state that there is no ordering between these two atomics. So they can be stored and fetched at any time the compiler or CPU thinks will be fine.