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