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
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 10, 2026
Update books

## rust-lang/book

1 commits in 39aeceaa3aeab845bc4517e7a44e48727d3b9dbe..05d114287b7d6f6c9253d5242540f00fbd6172ab
2026-02-03 15:19:04 UTC to 2026-02-03 15:19:04 UTC

- Temporarily remove the link to `Drop::drop` (rust-lang/book#4576)

## rust-lang/nomicon

1 commits in 050c002a360fa45b701ea34feed7a860dc8a41bf..b8f254a991b8b7e8f704527f0d4f343a4697dfa9
2026-01-29 12:15:01 UTC to 2026-01-29 12:15:01 UTC

- Fix deprecation warning for compare_and_swap in atomics.md (rust-lang/nomicon#519)

## rust-lang/reference

12 commits in 990819b86c22bbf538c0526f0287670f3dc1a67a..addd0602c819b6526b9cc97653b0fadca395528c
2026-02-04 14:35:59 UTC to 2026-01-26 18:02:14 UTC

- const-eval.const-expr.field: make paragraph more clear (rust-lang/reference#2157)
- make more clear what the link target is (rust-lang/reference#2156)
- Update two URLs (rust-lang/reference#2154)
- Add a chapter on divergence (rust-lang/reference#2067)
- Guarantee `repr(C)` union field offset (rust-lang/reference#2128)
- Reference updates for forbidding object lifetime changing pointer casts (rust-lang/reference#1951)
- Clarify only arrays undergo unsized coercion during dispatch (rust-lang/reference#2139)
- Split the textual chapter into separate char and str chapters (rust-lang/reference#2145)
- Document ppc inline asm support (rust-lang/reference#2056)
- Unwrap items, expressions, patterns, and types (rust-lang/reference#2141)
- undefined behavior: add missing plural in `undefined.misaligned.ptr` (rust-lang/reference#2146)
- inline-assembly: add a space to the `asm.abi-clobbers.many` example (rust-lang/reference#2144)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 10, 2026
Update books

## rust-lang/book

1 commits in 39aeceaa3aeab845bc4517e7a44e48727d3b9dbe..05d114287b7d6f6c9253d5242540f00fbd6172ab
2026-02-03 15:19:04 UTC to 2026-02-03 15:19:04 UTC

- Temporarily remove the link to `Drop::drop` (rust-lang/book#4576)

## rust-lang/nomicon

1 commits in 050c002a360fa45b701ea34feed7a860dc8a41bf..b8f254a991b8b7e8f704527f0d4f343a4697dfa9
2026-01-29 12:15:01 UTC to 2026-01-29 12:15:01 UTC

- Fix deprecation warning for compare_and_swap in atomics.md (rust-lang/nomicon#519)

## rust-lang/reference

12 commits in 990819b86c22bbf538c0526f0287670f3dc1a67a..addd0602c819b6526b9cc97653b0fadca395528c
2026-02-04 14:35:59 UTC to 2026-01-26 18:02:14 UTC

- const-eval.const-expr.field: make paragraph more clear (rust-lang/reference#2157)
- make more clear what the link target is (rust-lang/reference#2156)
- Update two URLs (rust-lang/reference#2154)
- Add a chapter on divergence (rust-lang/reference#2067)
- Guarantee `repr(C)` union field offset (rust-lang/reference#2128)
- Reference updates for forbidding object lifetime changing pointer casts (rust-lang/reference#1951)
- Clarify only arrays undergo unsized coercion during dispatch (rust-lang/reference#2139)
- Split the textual chapter into separate char and str chapters (rust-lang/reference#2145)
- Document ppc inline asm support (rust-lang/reference#2056)
- Unwrap items, expressions, patterns, and types (rust-lang/reference#2141)
- undefined behavior: add missing plural in `undefined.misaligned.ptr` (rust-lang/reference#2146)
- inline-assembly: add a space to the `asm.abi-clobbers.many` example (rust-lang/reference#2144)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 11, 2026
Update books

## rust-lang/book

1 commits in 39aeceaa3aeab845bc4517e7a44e48727d3b9dbe..05d114287b7d6f6c9253d5242540f00fbd6172ab
2026-02-03 15:19:04 UTC to 2026-02-03 15:19:04 UTC

- Temporarily remove the link to `Drop::drop` (rust-lang/book#4576)

## rust-lang/nomicon

1 commits in 050c002a360fa45b701ea34feed7a860dc8a41bf..b8f254a991b8b7e8f704527f0d4f343a4697dfa9
2026-01-29 12:15:01 UTC to 2026-01-29 12:15:01 UTC

- Fix deprecation warning for compare_and_swap in atomics.md (rust-lang/nomicon#519)

## rust-lang/reference

12 commits in 990819b86c22bbf538c0526f0287670f3dc1a67a..addd0602c819b6526b9cc97653b0fadca395528c
2026-02-04 14:35:59 UTC to 2026-01-26 18:02:14 UTC

- const-eval.const-expr.field: make paragraph more clear (rust-lang/reference#2157)
- make more clear what the link target is (rust-lang/reference#2156)
- Update two URLs (rust-lang/reference#2154)
- Add a chapter on divergence (rust-lang/reference#2067)
- Guarantee `repr(C)` union field offset (rust-lang/reference#2128)
- Reference updates for forbidding object lifetime changing pointer casts (rust-lang/reference#1951)
- Clarify only arrays undergo unsized coercion during dispatch (rust-lang/reference#2139)
- Split the textual chapter into separate char and str chapters (rust-lang/reference#2145)
- Document ppc inline asm support (rust-lang/reference#2056)
- Unwrap items, expressions, patterns, and types (rust-lang/reference#2141)
- undefined behavior: add missing plural in `undefined.misaligned.ptr` (rust-lang/reference#2146)
- inline-assembly: add a space to the `asm.abi-clobbers.many` example (rust-lang/reference#2144)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 11, 2026
Update books

## rust-lang/book

1 commits in 39aeceaa3aeab845bc4517e7a44e48727d3b9dbe..05d114287b7d6f6c9253d5242540f00fbd6172ab
2026-02-03 15:19:04 UTC to 2026-02-03 15:19:04 UTC

- Temporarily remove the link to `Drop::drop` (rust-lang/book#4576)

## rust-lang/nomicon

1 commits in 050c002a360fa45b701ea34feed7a860dc8a41bf..b8f254a991b8b7e8f704527f0d4f343a4697dfa9
2026-01-29 12:15:01 UTC to 2026-01-29 12:15:01 UTC

- Fix deprecation warning for compare_and_swap in atomics.md (rust-lang/nomicon#519)

## rust-lang/reference

12 commits in 990819b86c22bbf538c0526f0287670f3dc1a67a..addd0602c819b6526b9cc97653b0fadca395528c
2026-02-04 14:35:59 UTC to 2026-01-26 18:02:14 UTC

- const-eval.const-expr.field: make paragraph more clear (rust-lang/reference#2157)
- make more clear what the link target is (rust-lang/reference#2156)
- Update two URLs (rust-lang/reference#2154)
- Add a chapter on divergence (rust-lang/reference#2067)
- Guarantee `repr(C)` union field offset (rust-lang/reference#2128)
- Reference updates for forbidding object lifetime changing pointer casts (rust-lang/reference#1951)
- Clarify only arrays undergo unsized coercion during dispatch (rust-lang/reference#2139)
- Split the textual chapter into separate char and str chapters (rust-lang/reference#2145)
- Document ppc inline asm support (rust-lang/reference#2056)
- Unwrap items, expressions, patterns, and types (rust-lang/reference#2141)
- undefined behavior: add missing plural in `undefined.misaligned.ptr` (rust-lang/reference#2146)
- inline-assembly: add a space to the `asm.abi-clobbers.many` example (rust-lang/reference#2144)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 11, 2026
Update books

## rust-lang/book

1 commits in 39aeceaa3aeab845bc4517e7a44e48727d3b9dbe..05d114287b7d6f6c9253d5242540f00fbd6172ab
2026-02-03 15:19:04 UTC to 2026-02-03 15:19:04 UTC

- Temporarily remove the link to `Drop::drop` (rust-lang/book#4576)

## rust-lang/nomicon

1 commits in 050c002a360fa45b701ea34feed7a860dc8a41bf..b8f254a991b8b7e8f704527f0d4f343a4697dfa9
2026-01-29 12:15:01 UTC to 2026-01-29 12:15:01 UTC

- Fix deprecation warning for compare_and_swap in atomics.md (rust-lang/nomicon#519)

## rust-lang/reference

12 commits in 990819b86c22bbf538c0526f0287670f3dc1a67a..addd0602c819b6526b9cc97653b0fadca395528c
2026-02-04 14:35:59 UTC to 2026-01-26 18:02:14 UTC

- const-eval.const-expr.field: make paragraph more clear (rust-lang/reference#2157)
- make more clear what the link target is (rust-lang/reference#2156)
- Update two URLs (rust-lang/reference#2154)
- Add a chapter on divergence (rust-lang/reference#2067)
- Guarantee `repr(C)` union field offset (rust-lang/reference#2128)
- Reference updates for forbidding object lifetime changing pointer casts (rust-lang/reference#1951)
- Clarify only arrays undergo unsized coercion during dispatch (rust-lang/reference#2139)
- Split the textual chapter into separate char and str chapters (rust-lang/reference#2145)
- Document ppc inline asm support (rust-lang/reference#2056)
- Unwrap items, expressions, patterns, and types (rust-lang/reference#2141)
- undefined behavior: add missing plural in `undefined.misaligned.ptr` (rust-lang/reference#2146)
- inline-assembly: add a space to the `asm.abi-clobbers.many` example (rust-lang/reference#2144)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 11, 2026
Update books

## rust-lang/book

1 commits in 39aeceaa3aeab845bc4517e7a44e48727d3b9dbe..05d114287b7d6f6c9253d5242540f00fbd6172ab
2026-02-03 15:19:04 UTC to 2026-02-03 15:19:04 UTC

- Temporarily remove the link to `Drop::drop` (rust-lang/book#4576)

## rust-lang/nomicon

1 commits in 050c002a360fa45b701ea34feed7a860dc8a41bf..b8f254a991b8b7e8f704527f0d4f343a4697dfa9
2026-01-29 12:15:01 UTC to 2026-01-29 12:15:01 UTC

- Fix deprecation warning for compare_and_swap in atomics.md (rust-lang/nomicon#519)

## rust-lang/reference

12 commits in 990819b86c22bbf538c0526f0287670f3dc1a67a..addd0602c819b6526b9cc97653b0fadca395528c
2026-02-04 14:35:59 UTC to 2026-01-26 18:02:14 UTC

- const-eval.const-expr.field: make paragraph more clear (rust-lang/reference#2157)
- make more clear what the link target is (rust-lang/reference#2156)
- Update two URLs (rust-lang/reference#2154)
- Add a chapter on divergence (rust-lang/reference#2067)
- Guarantee `repr(C)` union field offset (rust-lang/reference#2128)
- Reference updates for forbidding object lifetime changing pointer casts (rust-lang/reference#1951)
- Clarify only arrays undergo unsized coercion during dispatch (rust-lang/reference#2139)
- Split the textual chapter into separate char and str chapters (rust-lang/reference#2145)
- Document ppc inline asm support (rust-lang/reference#2056)
- Unwrap items, expressions, patterns, and types (rust-lang/reference#2141)
- undefined behavior: add missing plural in `undefined.misaligned.ptr` (rust-lang/reference#2146)
- inline-assembly: add a space to the `asm.abi-clobbers.many` example (rust-lang/reference#2144)
rust-timer added a commit to rust-lang/rust that referenced this pull request Feb 12, 2026
Rollup merge of #152397 - rustbot:docs-update, r=ehuss

Update books

## rust-lang/book

1 commits in 39aeceaa3aeab845bc4517e7a44e48727d3b9dbe..05d114287b7d6f6c9253d5242540f00fbd6172ab
2026-02-03 15:19:04 UTC to 2026-02-03 15:19:04 UTC

- Temporarily remove the link to `Drop::drop` (rust-lang/book#4576)

## rust-lang/nomicon

1 commits in 050c002a360fa45b701ea34feed7a860dc8a41bf..b8f254a991b8b7e8f704527f0d4f343a4697dfa9
2026-01-29 12:15:01 UTC to 2026-01-29 12:15:01 UTC

- Fix deprecation warning for compare_and_swap in atomics.md (rust-lang/nomicon#519)

## rust-lang/reference

12 commits in 990819b86c22bbf538c0526f0287670f3dc1a67a..addd0602c819b6526b9cc97653b0fadca395528c
2026-02-04 14:35:59 UTC to 2026-01-26 18:02:14 UTC

- const-eval.const-expr.field: make paragraph more clear (rust-lang/reference#2157)
- make more clear what the link target is (rust-lang/reference#2156)
- Update two URLs (rust-lang/reference#2154)
- Add a chapter on divergence (rust-lang/reference#2067)
- Guarantee `repr(C)` union field offset (rust-lang/reference#2128)
- Reference updates for forbidding object lifetime changing pointer casts (rust-lang/reference#1951)
- Clarify only arrays undergo unsized coercion during dispatch (rust-lang/reference#2139)
- Split the textual chapter into separate char and str chapters (rust-lang/reference#2145)
- Document ppc inline asm support (rust-lang/reference#2056)
- Unwrap items, expressions, patterns, and types (rust-lang/reference#2141)
- undefined behavior: add missing plural in `undefined.misaligned.ptr` (rust-lang/reference#2146)
- inline-assembly: add a space to the `asm.abi-clobbers.many` example (rust-lang/reference#2144)
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