Skip to content

Conversation

@Unconnectable
Copy link
Contributor

Description:

The current example code triggers the following deprecation warning in recent Rust versions:

warning: use of deprecated method `std::sync::atomic::AtomicBool::compare_and_swap`: Use `compare_exchange` or `compare_exchange_weak` instead
  --> src/main.rs:11:16
   |
11 |     while lock.compare_and_swap(false, true, Ordering::Acquire) {}
   |                ^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(deprecated)]` on by default

Changes:

This PR replaces the deprecated compare_and_swap with compare_exchange:

// Before
while lock.compare_and_swap(false, true, Ordering::Acquire) { }

// After
while lock.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed).is_err() { }

Technical Note on compare_exchange vs compare_exchange_weak: While compare_exchange_weak is often preferred in loop scenarios for potential performance gains on certain architectures, I have opted for the standard compare_exchange here. This maintains exact semantic parity with the original example and avoids the need for additional explanations regarding "spurious failures," keeping the focus on memory ordering as intended in the nomicon.

The compare_and_swap method is deprecated in favor of compare_exchange. This update uses Ordering::Acquire for success and Ordering::Relaxed for failure, which is appropriate for a spinlock acquire loop.

Copy link
Member

@JohnTitor JohnTitor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@JohnTitor JohnTitor added this pull request to the merge queue Jan 29, 2026
Merged via the queue into rust-lang:master with commit b8f254a Jan 29, 2026
2 checks passed
@Unconnectable Unconnectable deleted the update/atomics-examples branch January 30, 2026 03:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants