Skip to content

Instantly share code, notes, and snippets.

@eval-exec
Created December 31, 2022 03:20
Show Gist options
  • Select an option

  • Save eval-exec/e6cf1eb0820ac3acdbacbbe46ebe598a to your computer and use it in GitHub Desktop.

Select an option

Save eval-exec/e6cf1eb0820ac3acdbacbbe46ebe598a to your computer and use it in GitHub Desktop.
can_this_panic.rs
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();
}
}
@eval-exec
Copy link
Copy Markdown
Author

Is it possible for this program to panic?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment