Skip to content

Instantly share code, notes, and snippets.

@dulimarta
Last active January 28, 2020 04:54
Show Gist options
  • Save dulimarta/5c9bca71fdbc3c3e48c5f9f5ad4bd62f to your computer and use it in GitHub Desktop.
Save dulimarta/5c9bca71fdbc3c3e48c5f9f5ad4bd62f to your computer and use it in GitHub Desktop.
CS452 Lab04 - Sample 2 (Rust)
// extern crate rand;
// use nix::unistd::sleep;
// use std::process;
use rand::Rng;
use std::thread;
fn main() {
// Use lambda when the thread function takes arguments
let thr1 = thread::spawn(|| do_greeting2(30));
let thr2 = thread::spawn(|| do_greeting2(50));
thr1.join().expect("Can't join");
thr2.join().expect("Can't join");
}
fn do_greeting2(limit: u32) {
let mut rng = rand::thread_rng();
let val = rng.gen_range(0, 2);
for k in 0..limit {
// sleep(1);
if val == 0 {
print!("{} Hello ", k);
} else {
println!("{} World", k);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment