test3.cc revision f56e876a
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 20f56e876aSShuo Chen int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 21f56e876aSShuo Chen muduo::Channel channel(&loop, timerfd); 22f56e876aSShuo Chen channel.setReadCallback(timeout); 23f56e876aSShuo Chen channel.enableReading(); 24f56e876aSShuo Chen 25f56e876aSShuo Chen struct itimerspec howlong; 26f56e876aSShuo Chen bzero(&howlong, sizeof howlong); 27f56e876aSShuo Chen howlong.it_value.tv_sec = 5; 28f56e876aSShuo Chen ::timerfd_settime(timerfd, 0, &howlong, NULL); 29f56e876aSShuo Chen 30cbe8e7d0SShuo Chen loop.loop(); 31f56e876aSShuo Chen 32f56e876aSShuo Chen ::close(timerfd); 33cbe8e7d0SShuo Chen} 34