extern crate libc; use std::mem::MaybeUninit; use std::thread; const NUM_THREADS: usize = 4; fn main() { let threads: Vec<_> = (0..NUM_THREADS) .map(|_| { thread::spawn(move || { let mut steps = 0; loop { steps += 1; if steps < 100_000 { continue; } steps = 0; let mut usage = MaybeUninit::::uninit(); unsafe { assert_eq!(libc::getrusage(libc::RUSAGE_THREAD, usage.as_mut_ptr()), 0); } let usage = unsafe { usage.assume_init() }.ru_utime; let us = usage.tv_sec * 1_000_000 + usage.tv_usec; if us >= 500_000 / NUM_THREADS as i64 { break; } } }) }) .collect(); for thread in threads { thread.join().unwrap(); } }