1#include "Channel.h" 2#include "EventLoop.h" 3 4#include <stdio.h> 5#include <sys/timerfd.h> 6 7muduo::EventLoop* g_loop; 8 9void timeout() 10{ 11 printf("Timeout!\n"); 12 g_loop->quit(); 13} 14 15int main() 16{ 17 muduo::EventLoop loop; 18 g_loop = &loop; 19 20 int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 21 muduo::Channel channel(&loop, timerfd); 22 channel.setReadCallback(timeout); 23 channel.enableReading(); 24 25 struct itimerspec howlong; 26 bzero(&howlong, sizeof howlong); 27 howlong.it_value.tv_sec = 5; 28 ::timerfd_settime(timerfd, 0, &howlong, NULL); 29 30 loop.loop(); 31 32 ::close(timerfd); 33} 34