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

推荐订阅源

WordPress大学
WordPress大学
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
Jina AI
Jina AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Y
Y Combinator Blog
V
Visual Studio Blog
C
Check Point Blog
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
量子位
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
M
MIT News - Artificial intelligence
爱范儿
爱范儿
B
Blog RSS Feed
MyScale Blog
MyScale Blog
H
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
美团技术团队
L
LangChain Blog
D
Docker

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.