Skip to content

Instantly share code, notes, and snippets.

@raphlinus
Created December 7, 2024 22:42
Show Gist options
  • Save raphlinus/81173b2c62c2481a487e0de86f87437d to your computer and use it in GitHub Desktop.
Save raphlinus/81173b2c62c2481a487e0de86f87437d to your computer and use it in GitHub Desktop.
Sketch of queue
use std::time::Duration;
use rand::Rng;
fn main() {
let (s, mut r) = ordered_channel::bounded(10);
for i in 0..100 {
let s_clone = s.clone();
rayon::spawn_fifo(move || {
let mut rng = rand::thread_rng();
let sleep_time = rng.gen_range(0..100);
std::thread::sleep(Duration::from_millis(sleep_time));
_ = s_clone.send(i, i);
});
std::thread::sleep(Duration::from_millis(10));
while let Ok(answer) = r.try_recv() {
println!("got answer {answer}");
}
}
drop(s);
println!("done spawning");
while let Ok(answer) = r.recv() {
println!("got answer {answer}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment