Created
July 12, 2018 17:46
-
-
Save rust-play/76d1da10beb83272a774fff444cababa to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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::thread; | |
use std::time::Duration; | |
fn main() { | |
concurrency_demo_1(); | |
concurrency_demo_2(); | |
} | |
fn concurrency_demo_1() { | |
let v = vec![1, 2, 3]; | |
let handle = thread::spawn(move || { | |
println!("Here's a vector: {:?}", v); | |
}); | |
handle.join().unwrap(); | |
} | |
use std::sync::mpsc; | |
fn concurrency_demo_2() { | |
let (i, o) = mpsc::channel(); | |
println!("Sending value."); | |
thread::spawn(move || { | |
let val1 = "hi".to_string(); | |
let val2 = "bye".to_string(); | |
thread::sleep(Duration::from_millis(1000)); | |
i.send(val1).unwrap(); | |
i.send(val2).unwrap(); | |
}); | |
let received = o.recv().unwrap(); | |
let received2 = o.recv().unwrap(); | |
println!("Got: {} and {}", received, received2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment