Skip to content

Instantly share code, notes, and snippets.

@pusateri
Created April 22, 2019 23:39
Show Gist options
  • Save pusateri/58ea591219ce2a90166daee811c3dbbb to your computer and use it in GitHub Desktop.
Save pusateri/58ea591219ce2a90166daee811c3dbbb to your computer and use it in GitHub Desktop.
mio extra timers don't fire
use std::time::Duration;
use mio::{Events, Ready, Poll, PollOpt, Token};
use mio_extras::channel;
use mio_extras::timer::Timer;
fn main() {
const CHANNEL: Token = Token(0);
let poll = Poll::new().unwrap();
let (tx, rx) = channel::channel::<u32>();
let mut events = Events::with_capacity(1024);
let mut tok = 1;
tx.send(1).unwrap();
poll.register(&rx, CHANNEL, Ready::readable(), PollOpt::edge()).unwrap();
loop {
poll.poll(&mut events, None).expect("poll.poll failed");
for event in events.iter() {
println!("event");
if event.token() == CHANNEL {
let _t: u32 = rx.try_recv().unwrap();
let when = Duration::from_secs(2);
let mut timer = Timer::default();
let timer_token = Token(tok);
println!("channel tok: {} when: {:?}", tok, when);
timer.set_timeout(when, tok);
tok += 1;
poll.register(&timer, timer_token, Ready::readable(), PollOpt::edge()).unwrap();
} else {
println!("timer: {:?}", event)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment