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 27int main() 28{ 29 printTid(); 30 muduo::EventLoop loop; 31 g_loop = &loop; 32 33 print("main"); 34 loop.runAfter(1, boost::bind(print, "once1")); 35 loop.runAfter(1.5, boost::bind(print, "once1.5")); 36 loop.runAfter(2.5, boost::bind(print, "once2.5")); 37 loop.runAfter(3.5, boost::bind(print, "once3.5")); 38 loop.runEvery(2, boost::bind(print, "every2")); 39 loop.runEvery(3, boost::bind(print, "every3")); 40 41 loop.loop(); 42 print("main loop exits"); 43 sleep(1); 44} 45