Created
December 14, 2024 17:41
-
-
Save eloff/3d5c51d0f7d828e1d420e3160ebbef3e to your computer and use it in GitHub Desktop.
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::{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