Skip to content

Commit 9dae0cc

Browse files
committed
---
yaml --- r: 224557 b: refs/heads/beta c: d1cf449 h: refs/heads/master i: 224555: bde550e v: v3
1 parent 68f72d4 commit 9dae0cc

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
@@ -23,7 +23,7 @@ refs/tags/0.9: 36870b185fc5f5486636d4515f0e22677493f225
2323
refs/tags/0.10: ac33f2b15782272ae348dbd7b14b8257b2148b5a
2424
refs/tags/0.11.0: e1247cb1d0d681be034adb4b558b5a0c0d5720f9
2525
refs/tags/0.12.0: f0c419429ef30723ceaf6b42f9b5a2aeb5d2e2d1
26-
refs/heads/beta: dbf3a63dd78f547bced8d086067d98ba0b414fd3
26+
refs/heads/beta: d1cf449034095725a19cba0bbc79753f2f460e69
2727
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
2828
refs/heads/tmp: 938f5d7af401e2d8238522fed4a612943b6e77fd
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f

branches/beta/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)