test3.cc revision 40161064
140161064SShuo Chen#include "Channel.h" 240161064SShuo Chen#include "EventLoop.h" 340161064SShuo Chen 440161064SShuo Chen#include <stdio.h> 540161064SShuo Chen#include <sys/timerfd.h> 640161064SShuo Chen 740161064SShuo Chenmuduo::EventLoop* g_loop; 840161064SShuo Chen 940161064SShuo Chenvoid timeout(muduo::Timestamp receiveTime) 1040161064SShuo Chen{ 1140161064SShuo Chen printf("%s Timeout!\n", receiveTime.toFormattedString().c_str()); 1240161064SShuo Chen g_loop->quit(); 1340161064SShuo Chen} 1440161064SShuo Chen 1540161064SShuo Chenint main() 1640161064SShuo Chen{ 1740161064SShuo Chen printf("%s started\n", muduo::Timestamp::now().toFormattedString().c_str()); 1840161064SShuo Chen muduo::EventLoop loop; 1940161064SShuo Chen g_loop = &loop; 2040161064SShuo Chen 2140161064SShuo Chen int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 2240161064SShuo Chen muduo::Channel channel(&loop, timerfd); 2340161064SShuo Chen channel.setReadCallback(timeout); 2440161064SShuo Chen channel.enableReading(); 2540161064SShuo Chen 2640161064SShuo Chen struct itimerspec howlong; 2740161064SShuo Chen bzero(&howlong, sizeof howlong); 2840161064SShuo Chen howlong.it_value.tv_sec = 5; 2940161064SShuo Chen ::timerfd_settime(timerfd, 0, &howlong, NULL); 3040161064SShuo Chen 3140161064SShuo Chen loop.loop(); 3240161064SShuo Chen 3340161064SShuo Chen ::close(timerfd); 3440161064SShuo Chen} 35