1354280cfSShuo Chen// copied from muduo/net/tests/TimerQueue_unittest.cc 2354280cfSShuo Chen 3354280cfSShuo Chen#include "EventLoop.h" 4354280cfSShuo Chen 5354280cfSShuo Chen#include <boost/bind.hpp> 6354280cfSShuo Chen 7354280cfSShuo Chen#include <stdio.h> 8354280cfSShuo Chen 9354280cfSShuo Chenint cnt = 0; 10354280cfSShuo Chenmuduo::EventLoop* g_loop; 11354280cfSShuo Chen 12354280cfSShuo Chenvoid printTid() 13354280cfSShuo Chen{ 14354280cfSShuo Chen printf("pid = %d, tid = %d\n", getpid(), muduo::CurrentThread::tid()); 15354280cfSShuo Chen printf("now %s\n", muduo::Timestamp::now().toString().c_str()); 16354280cfSShuo Chen} 17354280cfSShuo Chen 18354280cfSShuo Chenvoid print(const char* msg) 19354280cfSShuo Chen{ 20354280cfSShuo Chen printf("msg %s %s\n", muduo::Timestamp::now().toString().c_str(), msg); 21354280cfSShuo Chen if (++cnt == 20) 22354280cfSShuo Chen { 23354280cfSShuo Chen g_loop->quit(); 24354280cfSShuo Chen } 25354280cfSShuo Chen} 26354280cfSShuo Chen 27354280cfSShuo Chenmuduo::TimerId toCancel; 28354280cfSShuo Chenvoid cancelSelf() 29354280cfSShuo Chen{ 30354280cfSShuo Chen print("cancelSelf()"); 31354280cfSShuo Chen g_loop->cancel(toCancel); 32354280cfSShuo Chen} 33354280cfSShuo Chen 34354280cfSShuo Chenint main() 35354280cfSShuo Chen{ 36354280cfSShuo Chen printTid(); 37354280cfSShuo Chen muduo::EventLoop loop; 38354280cfSShuo Chen g_loop = &loop; 39354280cfSShuo Chen 40354280cfSShuo Chen print("main"); 41354280cfSShuo Chen loop.runAfter(1, boost::bind(print, "once1")); 42354280cfSShuo Chen loop.runAfter(1.5, boost::bind(print, "once1.5")); 43354280cfSShuo Chen loop.runAfter(2.5, boost::bind(print, "once2.5")); 44354280cfSShuo Chen loop.runAfter(3.5, boost::bind(print, "once3.5")); 45354280cfSShuo Chen muduo::TimerId t = loop.runEvery(2, boost::bind(print, "every2")); 46354280cfSShuo Chen loop.runEvery(3, boost::bind(print, "every3")); 47354280cfSShuo Chen loop.runAfter(10, boost::bind(&muduo::EventLoop::cancel, &loop, t)); 48354280cfSShuo Chen toCancel = loop.runEvery(5, cancelSelf); 49354280cfSShuo Chen 50354280cfSShuo Chen loop.loop(); 51354280cfSShuo Chen print("main loop exits"); 52354280cfSShuo Chen sleep(1); 53354280cfSShuo Chen} 54