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(muduo::Timestamp receiveTime) 10{ 11 printf("%s Timeout!\n", receiveTime.toFormattedString().c_str()); 12 g_loop->quit(); 13} 14 15int main() 16{ 17 printf("%s started\n", muduo::Timestamp::now().toFormattedString().c_str()); 18 muduo::EventLoop loop; 19 g_loop = &loop; 20 21 int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 22 muduo::Channel channel(&loop, timerfd); 23 channel.setReadCallback(timeout); 24 channel.enableReading(); 25 26 struct itimerspec howlong; 27 bzero(&howlong, sizeof howlong); 28 howlong.it_value.tv_sec = 5; 29 ::timerfd_settime(timerfd, 0, &howlong, NULL); 30 31 loop.loop(); 32 33 ::close(timerfd); 34} 35