Created
October 2, 2021 21:51
-
-
Save SwannHERRERA/7ea4c2d6eee88438791a416a51438e79 to your computer and use it in GitHub Desktop.
demo-thread-rust
This file contains hidden or 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::sync::Mutex; | |
use std::thread; | |
use std::time::Duration; | |
fn main() { | |
let mut handles = vec![]; | |
let fifty_millis = Duration::from_millis(50); | |
let forty_millis = Duration::from_millis(40); | |
let twenty_millis = Duration::from_millis(20); | |
let time_printing_underscore = Duration::from_secs(10); | |
let time_printing_star = Duration::from_secs(11); | |
let time_printing_degree = Duration::from_secs(9); | |
let handle_fifty = | |
thread::spawn(move || print_in_thread(time_printing_underscore, fifty_millis, '_')); | |
let handle_forty = | |
thread::spawn(move || print_in_thread(time_printing_star, forty_millis, '*')); | |
let handle_twenty = | |
thread::spawn(move || print_in_thread(time_printing_degree, twenty_millis, '°')); | |
handles.push(handle_fifty); | |
handles.push(handle_forty); | |
handles.push(handle_twenty); | |
for handle in handles { | |
handle.join().unwrap(); | |
} | |
} | |
fn print_in_thread( | |
mut time_printing: Duration, | |
duration_beween_print: Duration, | |
printed_char: char, | |
) { | |
while time_printing.as_secs() != 0 { | |
print!("{}", printed_char); | |
time_printing -= duration_beween_print; | |
} | |
thread::sleep(duration_beween_print); | |
} | |
// print into stdio should be lock but this is a nigthly feature and it's not the main purpose |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment