Last active
May 23, 2021 10:19
-
-
Save omern1/f54f7e728aa3480277845780df349f23 to your computer and use it in GitHub Desktop.
A naive implementation of a strange sorting algorithm
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
//https://twitter.com/fermatslibrary/status/937687947041701888 | |
use std::{thread, time}; | |
use std::sync::{Arc, Barrier}; | |
fn main() { | |
let elements = vec![9,8,7,6,5,4,3,2,1,0]; | |
sleep_sort(elements); | |
} | |
fn sleep_sort(dataset: Vec<i32>) { | |
let barrier = Arc::new(Barrier::new(dataset.len())); | |
let mut handles: Vec<std::thread::JoinHandle<()>> = Vec::with_capacity(dataset.len()); | |
for element in dataset { | |
let barrier = barrier.clone(); | |
handles.push(thread::spawn(move ||{ | |
barrier.wait(); | |
thread::sleep(time::Duration::from_millis(element as u64)); | |
println!("{}", element); | |
})); | |
} | |
for handle in handles { | |
handle.join().unwrap(); | |
} | |
} |
muhammadmuzzammil1998
commented
May 23, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment