use atomic_interval::AtomicIntervalLight;
use once_cell::sync::OnceCell;
use std::thread;
use std::time::Duration;
const MAX_PERIOD_SAMPLING: Duration = Duration::from_secs(1);
fn push_sample(id_thread: usize, value: u8) {
static LIMITER: OnceCell<AtomicIntervalLight> = OnceCell::new();
let limiter_init = || AtomicIntervalLight::new(MAX_PERIOD_SAMPLING);
if LIMITER.get_or_init(limiter_init).is_ticked() {
println!("Thread '{}' pushed sample: '{}'", id_thread, value);
}
}
fn main() {
let num_threads = num_cpus::get();
(0..num_threads)
.map(|id_thread| {
thread::spawn(move || loop {
let sample = rand::random();
push_sample(id_thread, sample);
thread::sleep(Duration::from_millis(1));
})
})
.collect::<Vec<_>>()
.into_iter()
.for_each(|join_handle| join_handle.join().unwrap());
}