test4.cc revision 65c497a3
165c497a3SShuo Chen// copied from muduo/net/tests/TimerQueue_unittest.cc 265c497a3SShuo Chen 365c497a3SShuo Chen#include "Channel.h" 465c497a3SShuo Chen#include "EventLoop.h" 565c497a3SShuo Chen 665c497a3SShuo Chen#include <boost/bind.hpp> 765c497a3SShuo Chen 865c497a3SShuo Chen#include <stdio.h> 965c497a3SShuo Chen 1065c497a3SShuo Chenint cnt = 0; 1165c497a3SShuo Chenmuduo::EventLoop* g_loop; 1265c497a3SShuo Chen 1365c497a3SShuo Chenvoid printTid() 1465c497a3SShuo Chen{ 1565c497a3SShuo Chen printf("pid = %d, tid = %d\n", getpid(), muduo::CurrentThread::tid()); 1665c497a3SShuo Chen printf("now %s\n", muduo::Timestamp::now().toString().c_str()); 1765c497a3SShuo Chen} 1865c497a3SShuo Chen 1965c497a3SShuo Chenvoid print(const char* msg) 2065c497a3SShuo Chen{ 2165c497a3SShuo Chen printf("msg %s %s\n", muduo::Timestamp::now().toString().c_str(), msg); 2265c497a3SShuo Chen if (++cnt == 20) 2365c497a3SShuo Chen { 2465c497a3SShuo Chen g_loop->quit(); 2565c497a3SShuo Chen } 2665c497a3SShuo Chen} 2765c497a3SShuo Chen 2865c497a3SShuo Chenint main() 2965c497a3SShuo Chen{ 3065c497a3SShuo Chen printTid(); 3165c497a3SShuo Chen muduo::EventLoop loop; 3265c497a3SShuo Chen g_loop = &loop; 3365c497a3SShuo Chen 3465c497a3SShuo Chen print("main"); 3565c497a3SShuo Chen loop.runAfter(1, boost::bind(print, "once1")); 3665c497a3SShuo Chen loop.runAfter(1.5, boost::bind(print, "once1.5")); 3765c497a3SShuo Chen loop.runAfter(2.5, boost::bind(print, "once2.5")); 3865c497a3SShuo Chen loop.runAfter(3.5, boost::bind(print, "once3.5")); 3965c497a3SShuo Chen loop.runEvery(2, boost::bind(print, "every2")); 4065c497a3SShuo Chen loop.runEvery(3, boost::bind(print, "every3")); 4165c497a3SShuo Chen 4265c497a3SShuo Chen loop.loop(); 4365c497a3SShuo Chen print("main loop exits"); 4465c497a3SShuo Chen sleep(1); 4565c497a3SShuo Chen} 46