Skip to content

Instantly share code, notes, and snippets.

@omern1
Last active May 23, 2021 10:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omern1/f54f7e728aa3480277845780df349f23 to your computer and use it in GitHub Desktop.
Save omern1/f54f7e728aa3480277845780df349f23 to your computer and use it in GitHub Desktop.
A naive implementation of a strange sorting algorithm
//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
Copy link

643cd5fb6f6b57719be90ff8102b1b07

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment