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