Skip to content

Commit

Permalink
Maybe ignore the explicit examples of a race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Gankra committed Jul 30, 2015
1 parent dbf3a63 commit d1cf449
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/doc/tarpl/races.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d1cf449

Please sign in to comment.