Last active
October 27, 2016 03:02
-
-
Save klardotsh/6977e5ace357256db80d138e8b36ebc7 to your computer and use it in GitHub Desktop.
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::sync::{ Arc, Mutex }; | |
use std::time::Duration; | |
fn kickoff_background(counter: &Arc<Mutex<u32>>) -> thread::JoinHandle<std::result::Result<(), ()>> { | |
let counter = counter.clone(); | |
return thread::spawn(move || { | |
loop { | |
let mut number = counter.lock().unwrap(); | |
*number += 1; | |
println!("Tick {}", *number); | |
if *number == 11 { | |
println!("Made it to 11"); | |
return Ok(()); | |
} | |
drop(number); | |
thread::sleep(Duration::from_millis(1000)); | |
} | |
}); | |
} | |
fn kickoff_offset(counter: &Arc<Mutex<u32>>) -> thread::JoinHandle<thread::Thread> { | |
let counter = counter.clone(); | |
return thread::spawn(move || { | |
loop { | |
thread::sleep(Duration::from_millis(500)); | |
let mut number = counter.lock().unwrap(); | |
*number += 1; | |
println!("Tock {}", *number); | |
drop(number); | |
thread::sleep(Duration::from_millis(500)); | |
} | |
}); | |
} | |
fn main() { | |
let counter = Arc::new(Mutex::new(0)); | |
let bg = kickoff_background(&counter); | |
kickoff_offset(&counter); | |
bg.join().unwrap().unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment