Skip to content

Instantly share code, notes, and snippets.

@supr
Created September 19, 2014 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save supr/db40fd9c731ec1c6efe5 to your computer and use it in GitHub Desktop.
Save supr/db40fd9c731ec1c6efe5 to your computer and use it in GitHub Desktop.
Simple Thread Implementations in Rust
use std::os;
fn main() {
let (tx, rx) = channel();
let args = os::args();
let mut max_messages: uint = 10u;
println!("Command line args len: {}", args.len());
if args.len() > 1 {
max_messages = match args.last() {
Some(n) => {
match from_str(n.as_slice().trim()) {
Some(num) => num,
None => 10u
}
},
None => 10u
}
}
println!("Max messages: {}", max_messages);
spawn(proc() {
let mut index = 1u;
loop {
println!("Sender -> Sending {}", index);
tx.send(index);
index = index+1;
if index > max_messages {
break;
}
}
});
spawn(proc() {
loop {
match rx.recv_opt() {
Ok(ref x) => println!("Receiver <- Received {}", *x),
Err(e) => {
println!("Remote Task Complted! {}", e);
break
}
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment