Created
February 28, 2019 14:00
-
-
Save rust-play/6a3cacf8cb3d680af88dd8a231087ca1 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::time::Duration; | |
use std::thread; | |
use std::sync::atomic::{AtomicBool, Ordering}; | |
use std::sync::Arc; | |
use std::sync::mpsc::channel; | |
fn long_running_function(x: u64, shared: &AtomicBool) -> Option<u64> { | |
for i in 0..x { | |
if shared.load(Ordering::Relaxed) { | |
return None | |
} | |
println!("{} at step {}", x, i); | |
thread::sleep(Duration::from_secs(x)); | |
} | |
shared.swap(true, Ordering::Relaxed); | |
Some(x * 2) | |
} | |
fn main() { | |
let (tx, rx) = channel(); | |
let shared = Arc::new(AtomicBool::new(false)); | |
for i in 1..8 { | |
let tx1 = tx.clone(); | |
let shared1 = Arc::clone(&shared); | |
thread::spawn(move || { | |
let result = long_running_function(i, &shared1); | |
tx1.send(result).unwrap() | |
}); | |
} | |
if let Ok(Some(n)) = rx.recv() { | |
println!("{}", n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment