test3.cc revision 65c497a3
165c497a3SShuo Chen#include "Channel.h" 265c497a3SShuo Chen#include "EventLoop.h" 365c497a3SShuo Chen 465c497a3SShuo Chen#include <stdio.h> 565c497a3SShuo Chen#include <sys/timerfd.h> 665c497a3SShuo Chen 765c497a3SShuo Chenmuduo::EventLoop* g_loop; 865c497a3SShuo Chen 965c497a3SShuo Chenvoid timeout() 1065c497a3SShuo Chen{ 1165c497a3SShuo Chen printf("Timeout!\n"); 1265c497a3SShuo Chen g_loop->quit(); 1365c497a3SShuo Chen} 1465c497a3SShuo Chen 1565c497a3SShuo Chenint main() 1665c497a3SShuo Chen{ 1765c497a3SShuo Chen muduo::EventLoop loop; 1865c497a3SShuo Chen g_loop = &loop; 1965c497a3SShuo Chen 2065c497a3SShuo Chen int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 2165c497a3SShuo Chen muduo::Channel channel(&loop, timerfd); 2265c497a3SShuo Chen channel.setReadCallback(timeout); 2365c497a3SShuo Chen channel.enableReading(); 2465c497a3SShuo Chen 2565c497a3SShuo Chen struct itimerspec howlong; 2665c497a3SShuo Chen bzero(&howlong, sizeof howlong); 2765c497a3SShuo Chen howlong.it_value.tv_sec = 5; 2865c497a3SShuo Chen ::timerfd_settime(timerfd, 0, &howlong, NULL); 2965c497a3SShuo Chen 3065c497a3SShuo Chen loop.loop(); 3165c497a3SShuo Chen 3265c497a3SShuo Chen ::close(timerfd); 3365c497a3SShuo Chen} 34