-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Blocking while popping #98
Comments
If you expect to be waiting for more than a few hundred nanoseconds, it's probably better to use a queue that has a proper blocking API? Maybe crossbeam::channel? If you are only waiting for a short time, you can use Another, not necessarily better way to do it: let x = loop {
if let Ok(item) = input.pop() {
break item;
}
// relax a while
}; And yet another, but this time definitely worse way of doing it: fn crazy(input: &mut rtrb::Consumer<i32>) -> i32 {
input.pop().unwrap_or_else(|_| {
// relax a while
crazy(input)
})
}
let x = crazy(&mut input); Apart from being crazy, this will also quickly lead to a stack overflow. |
In case anyone finds this useful, here's my approach: let backoff = crossbeam_utils::Backoff::new();
loop {
match consumer.pop() {
Ok(item) => {
backoff.reset();
// do stuff with `item`
// ...
}
Err(rtrb::PopError::Empty) => {
if backoff.is_completed() {
thread::sleep(Duration::from_micros(/* your desirable duration */));
} else {
backoff.snooze();
}
}
}
} This still keeps |
Is there a better way to block until you have something to pop other than:
?
The text was updated successfully, but these errors were encountered: