Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eloff/3d5c51d0f7d828e1d420e3160ebbef3e to your computer and use it in GitHub Desktop.
Save eloff/3d5c51d0f7d828e1d420e3160ebbef3e to your computer and use it in GitHub Desktop.
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;
fn main() {
static COUNTER: AtomicUsize = AtomicUsize::new(0);
let cores = core_affinity::get_core_ids().expect("Failed to get core IDs");
let writer_core = cores.first().unwrap().clone();
let reader_core = cores.last().unwrap().clone();
let writer = thread::spawn(move || {
core_affinity::set_for_current(writer_core);
for i in 1..=1_000_000_000 {
COUNTER.store(i, Ordering::Release);
}
});
let reader = thread::spawn(move || {
core_affinity::set_for_current(reader_core);
loop {
let value = COUNTER.load(Ordering::Acquire);
if value == 1_000_000_000 {
break;
}
}
println!("Counter reached 1 billion!");
});
writer.join().unwrap();
reader.join().unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment