use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};
use async_io::Timer;
use async_mutex::Mutex;
use futures_lite::future;
fn main() {
let num_threads = 30;
let mut threads = Vec::new();
let hits = Arc::new(Mutex::new(vec![0; num_threads]));
for i in 0..num_threads {
let hits = hits.clone();
threads.push(thread::spawn(move || {
future::block_on(async {
let start = Instant::now();
while start.elapsed() < Duration::from_secs(1) {
let mut hits = hits.lock().await;
hits[i] += 1;
Timer::after(Duration::from_micros(5000)).await;
}
})
}));
}
for t in threads {
t.join().unwrap();
}
dbg!(hits);
}