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

推荐订阅源

M
MIT News - Artificial intelligence
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
Last Week in AI
Last Week in AI
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学
The Cloudflare Blog
IT之家
IT之家
雷峰网
雷峰网
小众软件
小众软件
博客园 - 叶小钗
博客园 - 聂微东
爱范儿
爱范儿
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
博客园 - 【当耐特】
V
V2EX
博客园_首页
T
Tailwind CSS Blog

The Rust Programming Language Forum - Latest topics

Beginner building a Rust backend framework (AI-assisted) — feedback appreciated C++ to Rust -- Exceptions Re-exporting a trait with a custom derive_macro Gold linker is deprecated Why is using PhantomData valid in this case? Code review for Static Pool Allocator Looking for a pool based allocator Why is this legal? What does it mean for Rustc to run "out of TLS keys"? Using async/await internally with no internal runtime, but exposing a nonblocking poll API — is this a reasonable design? Testing functions that use randomness `cargo-path`: improve coding agents&#39; ability to find Rust documentation Rust task runners Lifetime weird case Windows - USB device not detected A random rustc-ice-[...].txt file appeared Develop rust where the environment is setup in a docker Undefined Behavior: in-bounds pointer arithmetic failed: attempting to offset pointer by 20 bytes, but got alloc238 which is only 1 byte from the end of the allocation Unbug 0.5 - Runtime debug assertions Is there a tiny error in section 6.2 Reference types? Lifetime woes implementing ratatui::Widget for a reference Rusqlite + Chrono: How do I simplify code to obtain chrono datetime value From OOP to Rust – struggling with code organization and data structure design Way to avoid a self-referential struct Rust RF and audio resources/communities Whyhttp - HTTP mocks that fail where the bug actually is Using tokio channel permits in a tower service Arc::increment_strong_count design question (cross-post) `&T`, `&mut T`, `Pin<&mut T>` and `&Cell<T>`: Ways of Borrowing a `T` Ratatui detect arrow key press and release
Is RMW operation less prone to reading stale values than ...
xmh0511 · 2026-04-17 · via The Rust Programming Language Forum - Latest topics

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.