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