Skip to content

Instantly share code, notes, and snippets.

@blippy
Created June 16, 2016 08:25
Show Gist options
  • Select an option

  • Save blippy/3c3bf87596e9151c6a74bb28b6c5ce01 to your computer and use it in GitHub Desktop.

Select an option

Save blippy/3c3bf87596e9151c6a74bb28b6c5ce01 to your computer and use it in GitHub Desktop.
Simple threads in rust
//#![feature(old_io)]
use std::thread;
use std::old_io::timer;
use std::time::duration::Duration;
fn main() {
let vin = vec![1.4f64, 1.2f64, 1.5f64];
let mut guards = Vec::with_capacity(3);
for i in 0..3 {
let inval = vin[i];
guards.push(thread::scoped( move || {
let ms = (1000.0f64 * inval) as i64;
let d = Duration::milliseconds(ms);
timer::sleep(d);
println!("Waited {}", inval);
10.0f64 + inval
}));
}
let mut answers = Vec::with_capacity(3);
for guard in guards {
let answer = guard.join();
answers.push(answer);
//println!("{}", answer);
};
for ans in answers { println!("{}", ans) } ;
}
@blippy

blippy commented Jun 16, 2016

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment