Skip to content

Commit d1d85f8

Browse files
committed
---
yaml --- r: 236140 b: refs/heads/stable c: d1cf449 h: refs/heads/master v: v3
1 parent 5b7f0a6 commit d1d85f8

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ refs/heads/tmp: afae2ff723393b3ab4ccffef6ac7c6d1809e2da0
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f
3030
refs/tags/homu-tmp: f859507de8c410b648d934d8f5ec1c52daac971d
3131
refs/tags/1.0.0-beta: 8cbb92b53468ee2b0c2d3eeb8567005953d40828
32-
refs/heads/stable: dbf3a63dd78f547bced8d086067d98ba0b414fd3
32+
refs/heads/stable: d1cf449034095725a19cba0bbc79753f2f460e69
3333
refs/tags/1.0.0: 55bd4f8ff2b323f317ae89e254ce87162d52a375
3434
refs/tags/1.1.0: bc3c16f09287e5545c1d3f76b7abd54f2eca868b
3535
refs/tags/1.2.0: f557861f822c34f07270347b94b5280de20a597e

branches/stable/src/doc/tarpl/races.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ race condition can't violate memory safety in a Rust program on
2525
its own. Only in conjunction with some other unsafe code can a race condition
2626
actually violate memory safety. For instance:
2727

28-
```rust
28+
```rust,norun
2929
use std::thread;
3030
use std::sync::atomic::{AtomicUsize, Ordering};
3131
use std::sync::Arc;
@@ -54,6 +54,24 @@ thread::spawn(move || {
5454
// program execution (panicing is rarely correct) depends on order of
5555
// thread execution.
5656
println!("{}", data[idx.load(Ordering::SeqCst)]);
57+
```
58+
59+
```rust,norun
60+
use std::thread;
61+
use std::sync::atomic::{AtomicUsize, Ordering};
62+
use std::sync::Arc;
63+
64+
let data = vec![1, 2, 3, 4];
65+
66+
let idx = Arc::new(AtomicUsize::new(0));
67+
let other_idx = idx.clone();
68+
69+
// `move` captures other_idx by-value, moving it into this thread
70+
thread::spawn(move || {
71+
// It's ok to mutate idx because this value
72+
// is an atomic, so it can't cause a Data Race.
73+
other_idx.fetch_add(10, Ordering::SeqCst);
74+
});
5775
5876
if idx.load(Ordering::SeqCst) < data.len() {
5977
unsafe {

0 commit comments

Comments
 (0)