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