diff options
| author | bors <bors@rust-lang.org> | 2015-07-30 16:18:32 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-07-30 16:18:32 +0000 |
| commit | 28869d45dd1d3ba04e17dec82408f9f3c548e776 (patch) | |
| tree | 0540eb85f67da778a2661b2a962afed6285828d4 | |
| parent | dbf3a63dd78f547bced8d086067d98ba0b414fd3 (diff) | |
| parent | d1cf449034095725a19cba0bbc79753f2f460e69 (diff) | |
| download | rust-28869d45dd1d3ba04e17dec82408f9f3c548e776.tar.gz rust-28869d45dd1d3ba04e17dec82408f9f3c548e776.zip | |
Auto merge of #27399 - Gankro:race, r=alexcrichton
r? @alexcrichton
| -rw-r--r-- | src/doc/tarpl/races.md | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/src/doc/tarpl/races.md b/src/doc/tarpl/races.md index 240e4aca7f6..2ad62c14a80 100644 --- a/src/doc/tarpl/races.md +++ b/src/doc/tarpl/races.md @@ -25,7 +25,7 @@ race condition can't violate memory safety in a Rust program on its own. Only in conjunction with some other unsafe code can a race condition actually violate memory safety. For instance: -```rust +```rust,norun use std::thread; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; @@ -54,6 +54,24 @@ thread::spawn(move || { // program execution (panicing is rarely correct) depends on order of // thread execution. println!("{}", data[idx.load(Ordering::SeqCst)]); +``` + +```rust,norun +use std::thread; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::Arc; + +let data = vec![1, 2, 3, 4]; + +let idx = Arc::new(AtomicUsize::new(0)); +let other_idx = idx.clone(); + +// `move` captures other_idx by-value, moving it into this thread +thread::spawn(move || { + // It's ok to mutate idx because this value + // is an atomic, so it can't cause a Data Race. + other_idx.fetch_add(10, Ordering::SeqCst); +}); if idx.load(Ordering::SeqCst) < data.len() { unsafe { |
