1// copied from muduo/net/tests/TimerQueue_unittest.cc 2 3#include "EventLoop.h" 4 5#include <boost/bind.hpp> 6 7#include <stdio.h> 8 9int cnt = 0; 10muduo::EventLoop* g_loop; 11 12void printTid() 13{ 14 printf("pid = %d, tid = %d\n", getpid(), muduo::CurrentThread::tid()); 15 printf("now %s\n", muduo::Timestamp::now().toString().c_str()); 16} 17 18void print(const char* msg) 19{ 20 printf("msg %s %s\n", muduo::Timestamp::now().toString().c_str(), msg); 21 if (++cnt == 20) 22 { 23 g_loop->quit(); 24 } 25} 26 27muduo::TimerId toCancel; 28void cancelSelf() 29{ 30 print("cancelSelf()"); 31 g_loop->cancel(toCancel); 32} 33 34int main() 35{ 36 printTid(); 37 muduo::EventLoop loop; 38 g_loop = &loop; 39 40 print("main"); 41 loop.runAfter(1, boost::bind(print, "once1")); 42 loop.runAfter(1.5, boost::bind(print, "once1.5")); 43 loop.runAfter(2.5, boost::bind(print, "once2.5")); 44 loop.runAfter(3.5, boost::bind(print, "once3.5")); 45 muduo::TimerId t = loop.runEvery(2, boost::bind(print, "every2")); 46 loop.runEvery(3, boost::bind(print, "every3")); 47 loop.runAfter(10, boost::bind(&muduo::EventLoop::cancel, &loop, t)); 48 toCancel = loop.runEvery(5, cancelSelf); 49 50 loop.loop(); 51 print("main loop exits"); 52 sleep(1); 53} 54