-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleepers.rs
38 lines (32 loc) · 965 Bytes
/
sleepers.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#![feature(scoped_threads)]
use std::{thread, time::Duration};
use scoped_threadpool_std::ThreadPool;
pub const NS_PER_SEC: Duration = Duration::from_nanos(1_000_000_000);
pub const NUM_SLEEPS: usize = 10;
pub fn usage(args: &[String]) {
eprintln!("Usage: {} <# of threads>", args[2]);
std::process::exit(1);
}
pub fn sleeper(dur: Duration) {
thread::sleep(dur);
}
pub fn main() {
let args: Vec<String> = std::env::args().collect();
if args[1] != "2" {
usage(&args);
}
let num_threads = args[1].parse::<usize>().unwrap();
if num_threads == 0 {
usage(&args);
}
let result = std::time::Instant::now();
thread::scope(|scope| {
let pool = ThreadPool::new(num_threads, scope);
for _ in 0..NUM_SLEEPS {
pool.execute(|| sleeper(Duration::from_nanos(0)));
}
});
let time_passed = result.elapsed();
println!("{:#?}", time_passed);
std::process::exit(0);
}