Created
December 7, 2024 22:42
-
-
Save raphlinus/81173b2c62c2481a487e0de86f87437d to your computer and use it in GitHub Desktop.
Sketch of queue
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::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