1cc7f415cSShuo Chen#include "Channel.h" 2cc7f415cSShuo Chen#include "EventLoop.h" 3cc7f415cSShuo Chen 4cc7f415cSShuo Chen#include <stdio.h> 5cc7f415cSShuo Chen#include <sys/timerfd.h> 6cc7f415cSShuo Chen 7cc7f415cSShuo Chenmuduo::EventLoop* g_loop; 8cc7f415cSShuo Chen 9cc7f415cSShuo Chenvoid timeout() 10cc7f415cSShuo Chen{ 11cc7f415cSShuo Chen printf("Timeout!\n"); 12cc7f415cSShuo Chen g_loop->quit(); 13cc7f415cSShuo Chen} 14cc7f415cSShuo Chen 15cc7f415cSShuo Chenint main() 16cc7f415cSShuo Chen{ 17cc7f415cSShuo Chen muduo::EventLoop loop; 18cc7f415cSShuo Chen g_loop = &loop; 19cc7f415cSShuo 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 30cc7f415cSShuo Chen loop.loop(); 31f56e876aSShuo Chen 32f56e876aSShuo Chen ::close(timerfd); 33cc7f415cSShuo Chen} 34