Created
December 31, 2022 03:20
-
-
Save eval-exec/e6cf1eb0820ac3acdbacbbe46ebe598a to your computer and use it in GitHub Desktop.
can_this_panic.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::atomic::{AtomicU64, Ordering}; | |
use std::sync::Arc; | |
use std::thread; | |
fn work() { | |
let a: Arc<AtomicU64> = Arc::new(AtomicU64::new(0)); | |
let b: Arc<AtomicU64> = Arc::new(AtomicU64::new(0)); | |
let j1 = thread::spawn({ | |
let (a, b) = (Arc::clone(&a), Arc::clone(&b)); | |
move || { | |
a.store(10, Ordering::Relaxed); | |
b.store(20, Ordering::Relaxed); | |
} | |
}); | |
let j2 = thread::spawn({ | |
move || { | |
let y = b.load(Ordering::Relaxed); | |
let x = a.load(Ordering::Relaxed); | |
if (x, y) == (0, 20) { | |
panic!("{x},{y}"); | |
} | |
} | |
}); | |
j1.join().unwrap(); | |
j2.join().unwrap(); | |
} | |
fn main() { | |
loop { | |
work(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is it possible for this program to panic?