Created
September 19, 2014 05:22
-
-
Save supr/db40fd9c731ec1c6efe5 to your computer and use it in GitHub Desktop.
Simple Thread Implementations in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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