25 lines
852 B
Rust
25 lines
852 B
Rust
// compile-flags: -Zmiri-disable-isolation
|
|
// ignore-windows: Concurrency on Windows is not supported yet.
|
|
use std::sync::Arc;
|
|
use std::sync::atomic::{AtomicUsize, Ordering, fence};
|
|
use std::time::Duration;
|
|
use std::thread;
|
|
|
|
fn main() {
|
|
static mut V: u32 = 0;
|
|
let a = Arc::new(AtomicUsize::default());
|
|
let b = a.clone();
|
|
thread::spawn(move || {
|
|
unsafe { V = 1 }
|
|
b.store(1, Ordering::SeqCst);
|
|
});
|
|
thread::sleep(Duration::from_millis(100));
|
|
fence(Ordering::SeqCst);
|
|
// Imagine the other thread's actions happening here.
|
|
assert_eq!(a.load(Ordering::Relaxed), 1);
|
|
// The fence is useless, since it did not happen-after the `store` in the other thread.
|
|
// Hence this is a data race.
|
|
// Also see https://github.com/rust-lang/miri/issues/2192.
|
|
unsafe { V = 2 } //~ERROR Data race detected
|
|
}
|