File tree Expand file tree Collapse file tree 2 files changed +20
-2
lines changed
branches/stable/src/doc/tarpl Expand file tree Collapse file tree 2 files changed +20
-2
lines changed Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ refs/heads/tmp: afae2ff723393b3ab4ccffef6ac7c6d1809e2da0
29
29
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f
30
30
refs/tags/homu-tmp: f859507de8c410b648d934d8f5ec1c52daac971d
31
31
refs/tags/1.0.0-beta: 8cbb92b53468ee2b0c2d3eeb8567005953d40828
32
- refs/heads/stable: dbf3a63dd78f547bced8d086067d98ba0b414fd3
32
+ refs/heads/stable: d1cf449034095725a19cba0bbc79753f2f460e69
33
33
refs/tags/1.0.0: 55bd4f8ff2b323f317ae89e254ce87162d52a375
34
34
refs/tags/1.1.0: bc3c16f09287e5545c1d3f76b7abd54f2eca868b
35
35
refs/tags/1.2.0: f557861f822c34f07270347b94b5280de20a597e
Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ race condition can't violate memory safety in a Rust program on
25
25
its own. Only in conjunction with some other unsafe code can a race condition
26
26
actually violate memory safety. For instance:
27
27
28
- ``` rust
28
+ ``` rust,norun
29
29
use std::thread;
30
30
use std::sync::atomic::{AtomicUsize, Ordering};
31
31
use std::sync::Arc;
@@ -54,6 +54,24 @@ thread::spawn(move || {
54
54
// program execution (panicing is rarely correct) depends on order of
55
55
// thread execution.
56
56
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
+ });
57
75
58
76
if idx.load(Ordering::SeqCst) < data.len() {
59
77
unsafe {
You can’t perform that action at this time.
0 commit comments