test4.cc revision f4e8e3d3
140161064SShuo Chen// copied from muduo/net/tests/TimerQueue_unittest.cc 240161064SShuo Chen 340161064SShuo Chen#include "Channel.h" 440161064SShuo Chen#include "EventLoop.h" 540161064SShuo Chen 640161064SShuo Chen#include <boost/bind.hpp> 740161064SShuo Chen 840161064SShuo Chen#include <stdio.h> 940161064SShuo Chen 1040161064SShuo Chenint cnt = 0; 1140161064SShuo Chenmuduo::EventLoop* g_loop; 1240161064SShuo Chen 1340161064SShuo Chenvoid printTid() 1440161064SShuo Chen{ 1540161064SShuo Chen printf("pid = %d, tid = %d\n", getpid(), muduo::CurrentThread::tid()); 1640161064SShuo Chen printf("now %s\n", muduo::Timestamp::now().toString().c_str()); 1740161064SShuo Chen} 1840161064SShuo Chen 1940161064SShuo Chenvoid print(const char* msg) 2040161064SShuo Chen{ 2140161064SShuo Chen printf("msg %s %s\n", muduo::Timestamp::now().toString().c_str(), msg); 2240161064SShuo Chen if (++cnt == 20) 2340161064SShuo Chen { 2440161064SShuo Chen g_loop->quit(); 2540161064SShuo Chen } 2640161064SShuo Chen} 2740161064SShuo Chen 28f4e8e3d3SShuo Chenmuduo::TimerId toCancel; 29f4e8e3d3SShuo Chenvoid cancelSelf() 30f4e8e3d3SShuo Chen{ 31f4e8e3d3SShuo Chen print("cancelSelf()"); 32f4e8e3d3SShuo Chen g_loop->cancel(toCancel); 33f4e8e3d3SShuo Chen} 34f4e8e3d3SShuo Chen 3540161064SShuo Chenint main() 3640161064SShuo Chen{ 3740161064SShuo Chen printTid(); 3840161064SShuo Chen muduo::EventLoop loop; 3940161064SShuo Chen g_loop = &loop; 4040161064SShuo Chen 4140161064SShuo Chen print("main"); 4240161064SShuo Chen loop.runAfter(1, boost::bind(print, "once1")); 4340161064SShuo Chen loop.runAfter(1.5, boost::bind(print, "once1.5")); 4440161064SShuo Chen loop.runAfter(2.5, boost::bind(print, "once2.5")); 4540161064SShuo Chen loop.runAfter(3.5, boost::bind(print, "once3.5")); 46f4e8e3d3SShuo Chen muduo::TimerId t = loop.runEvery(2, boost::bind(print, "every2")); 4740161064SShuo Chen loop.runEvery(3, boost::bind(print, "every3")); 48f4e8e3d3SShuo Chen loop.runAfter(10, boost::bind(&muduo::EventLoop::cancel, &loop, t)); 49f4e8e3d3SShuo Chen toCancel = loop.runEvery(5, cancelSelf); 5040161064SShuo Chen 5140161064SShuo Chen loop.loop(); 5240161064SShuo Chen print("main loop exits"); 5340161064SShuo Chen sleep(1); 5440161064SShuo Chen} 55