14792573fSShuo Chen#include "Channel.h" 24792573fSShuo Chen#include "EventLoop.h" 34792573fSShuo Chen 44792573fSShuo Chen#include <stdio.h> 54792573fSShuo Chen#include <sys/timerfd.h> 64792573fSShuo Chen 74792573fSShuo Chenmuduo::EventLoop* g_loop; 84792573fSShuo Chen 94792573fSShuo Chenvoid timeout() 104792573fSShuo Chen{ 114792573fSShuo Chen printf("Timeout!\n"); 124792573fSShuo Chen g_loop->quit(); 134792573fSShuo Chen} 144792573fSShuo Chen 154792573fSShuo Chenint main() 164792573fSShuo Chen{ 174792573fSShuo Chen muduo::EventLoop loop; 184792573fSShuo Chen g_loop = &loop; 194792573fSShuo Chen 204792573fSShuo Chen int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 214792573fSShuo Chen muduo::Channel channel(&loop, timerfd); 224792573fSShuo Chen channel.setReadCallback(timeout); 234792573fSShuo Chen channel.enableReading(); 244792573fSShuo Chen 254792573fSShuo Chen struct itimerspec howlong; 264792573fSShuo Chen bzero(&howlong, sizeof howlong); 274792573fSShuo Chen howlong.it_value.tv_sec = 5; 284792573fSShuo Chen ::timerfd_settime(timerfd, 0, &howlong, NULL); 294792573fSShuo Chen 304792573fSShuo Chen loop.loop(); 314792573fSShuo Chen 324792573fSShuo Chen ::close(timerfd); 334792573fSShuo Chen} 34